Android导航抽屉活动

时间:2016-11-18 09:57:11

标签: android android-fragments navigation-drawer

我在我的应用中使用导航抽屉活动,我有一点问题。 当我在导航项目上选择项目时,菜单图标会变为箭头,如下图所示: enter image description here

我想要的是这个:

enter image description here

第二件事,当从主片段导航到子片段时,如何在单击箭头图标时启用后退功能?

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    displayView();

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    Fragment fragment = null;
    int id = item.getItemId();
    String title ="";
    boolean isOK = true;
    if (id == R.id.nav_home) {
        // Handle the camera action
         fragment = new HomeFragment();
        title = getString(R.string.nav_item_home);
    } else if (id == R.id.nav_store) {
        if (MyHelper.licationServiceIsActive(MainActivity.this)){
        fragment = new MagasinsFragment();
        title = getString(R.string.nav_item_store);}else {
            isOK = false;
        }

    } else if (id == R.id.nav_catalog) {
        fragment = new AllCatalogueFragment();
        title = getString(R.string.nav_item_catalog);

    } else if (id == R.id.nav_parking) {
        if (MyHelper.licationServiceIsActive(MainActivity.this)){
        fragment = new ParkingFragment();
        title = getString(R.string.nav_item_parking);}else {
            isOK = false;
        }

    } else if (id == R.id.nav_cart) {
        fragment = new AllListFragment();
        title = getString(R.string.nav_item_cart);
        //fragment = new ListeFragment();
    } else if (id == R.id.nav_settings) {
        fragment = new SettingsFragment();
        title = getString(R.string.action_settings);
    }
    if (isOK) {
if (fragment != null) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    for (int i = 0; i < count; ++i) {
        fragmentManager.popBackStack();
    }
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container_body, fragment);


    fragmentTransaction.commit();

    // set the toolbar title
    getSupportActionBar().setTitle(title);
}
  }else {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Information");
dialog.setMessage("Le service de localisation est désactivé.\nVoulez-vous l'activer?");
dialog.setPositiveButton("Activer", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(myIntent);
        //get gps
    }
});
dialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
        // TODO Auto-generated method stub

    }
});
dialog.show();
   }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);

    return true;
}

3 个答案:

答案 0 :(得分:2)

我认为您应该停用ActionBarToolbar

的向上图标

ActionBar

getActionBar().setDisplayHomeAsUpEnabled(false);

答案 1 :(得分:0)

对于第一个问题: 您可能正在使用ActionBarDrawerToggle方法,因为它将菜单按钮更改为后退按钮!如果您正在使用它,请删除该代码。

第二个问题:

要在片段中实现Back功能,您只需要PopBackStack()

例如:

   public void moveBack()
    {
//FM =FragmentManager object
        if(FM!=null && FM.getBackStackEntryCount()>0)
       {
         FM.popBackStack() //to pop fragment from backstack
       }
       else
       {
         finish();  //for activity exit
       }
    }

确保将片段添加到BackStack。

答案 2 :(得分:0)

要浏览BAck按钮,请尝试此操作,在后退按钮上调用OnBackPressed();方法以返回上一个活动。