简体   繁体   中英

How to hide navigation drawer item while the respective fragment is opened

In my app, I used navigation drawer. Here I listed all the items.

From the image,the items are,

  • Home
  • Filter & Sort
  • WishList
  • Shop
  • MyOrder
  • Settings
  • LogOut

If I am in the Fragment of Shop , I need to hide it. How to do this?

Please help me.

在此处输入图像描述

You can handle it in fragments onAttach method. Set the visibility of perticular item according to your need.

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    YourActivity activity = (YourActivity)context;
    NavigationView navigationView = (NavigationView) activity.findViewById(R.id.yournavigationviewid);
    navigationView.getMenu().findItem(R.id.youritemid).setVisible(false);
}

inside your setNavigationItemSelectedListener where you get the selected menuItem, you can implement the code. Also you require to store the instance of the hidden menu item to make it visible later

MenuItem prevMenuItem;

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

   @Override
   public boolean onNavigationItemSelected(MenuItem menuItem) {
      if(prevMenuItem != null) prevMenuItem.setVisible(true) //making visible the previously hidden item.
      menuItem.setVisible(false);  

      prevMenuItem = menuItem //storing the instance of currently hidden item to make it visible later.

      return true;
   }
});

In your public onNavigationItemSelected(MenuItem item) if you are setting one fragment then automatically drawer will hide. I'm doing like this :

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    toolbar.setTitle(item.toString());
    int id = item.getItemId();
    if (id == R.id.dashboard) {
        fragment = new DashboardFragment();

    } else if (id == R.id.manage_users) {

    }else{
    }
    setFragmentLayout(fragment);
    return true;
}

Set your fragment according to your requirements.

You can hide the drawer using mDrawerLayout.closeDrawers() in onNavigationItemSelected Listener like this:

    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {
                    case R.id.navigation_item_shop:
                        //do your stuffs or attach fragment
                        mDrawerLayout.closeDrawers();
                        return true;
                    default:
                        return true;
               }
         }
   }

on the fragments overide the onAttach method. Set the visibility of for items your don't need.

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    MainActivity activity = (MainActivity)context;
    NavigationView navigationView = (NavigationView) activity.findViewById(R.id.navmenu);
   // hide the menu items not related to this fragment
    Menu m = navigationView.getMenu();
    m.findItem(R.id.first).setVisible(false);
    m.findItem(R.id.second).setVisible(false);
    m.findItem(R.id.therd).setVisible(false);
    //and so on

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM