How can I get menu item in NavigationView?

試著忘記壹切 提交于 2019-12-03 01:01:36

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

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)

just use below way

nav_draw = (NavigationView) findViewById(R.id.nav_draw);
nav_draw.getMenu().findItem(R.id.navigation_item_1).setVisible(false);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!