简体   繁体   English

如何在打开相应的片段时隐藏导航抽屉项目

[英]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.如果我在Shop的 Fragment 中,我需要隐藏它。 How to do this?这个怎么做?

Please help me.请帮我。

在此处输入图像描述

You can handle it in fragments onAttach method.您可以在片段onAttach方法中处理它。 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.在您获取所选菜单项的 setNavigationItemSelectedListener 中,您可以实现代码。 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.在您的公共 onNavigationItemSelected(MenuItem item) 中,如果您正在设置一个片段,那么抽屉将自动隐藏。 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:您可以在onNavigationItemSelected Listener 中使用mDrawerLayout.closeDrawers()隐藏抽屉,如下所示:

    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.在片段上覆盖onAttach方法。 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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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