How can I get menu item in NavigationView?

后端 未结 5 1714
眼角桃花
眼角桃花 2021-02-05 00:28


        
相关标签:
5条回答
  • 2021-02-05 00:51

    just use below way

    nav_draw = (NavigationView) findViewById(R.id.nav_draw);
    nav_draw.getMenu().findItem(R.id.navigation_item_1).setVisible(false);
    
    0 讨论(0)
  • 2021-02-05 00:51

    You can get the item by

    navigationView.Menu.GetItem(menu_index)
    
    0 讨论(0)
  • 2021-02-05 00:52
    View navHeader=navigationView.getHeaderView(0);[![enter image description here][1]][1]
    

    navMenu=navigationView.getMenu();

    1. List item

      //work for login and logout menu
      if (MyPreferences.getBoolean(BaseActivity.this,ConstantValues.KEY_IS_LOGIN)){
          navMenu.getItem(0).setVisible(false);
          navMenu.getItem(9).setVisible(true);
      }else {
          navMenu.getItem(0).setVisible(true);
          navMenu.getItem(9).setVisible(false);
      }
      
    0 讨论(0)
  • 2021-02-05 00:53

    You can get that by method of NavigationView.getMenu()

    Menu menuNav = mNavigationView.getMenu();
    

    Then you can find specific item by

    MenuItem logoutItem = menuNav.findItem(R.id.menu_logout);
    

    See Official documentation for NavigationView

    0 讨论(0)
  • 2021-02-05 01:13

    I think you should first get the menu like this:

    navigationView.getMenu().findItem(R.id.login).setVisible(false);
    

    The main aspect of this code is calling navigationView.getMenu() to get the menu from this you will have a reference of current inflated menu and call findViewById() and after that you can whatever you what.

    For Group of item ex:-

    <group
    android:id="@+id/group_1"
    android:checkableBehavior="single"
    android:visible="false">
    ...
    

    and then you can control this using :-

    navigationView.getMenu().setGroupVisible(R.id.group_1, true)
    
    0 讨论(0)
提交回复
热议问题