Change NavigationView items when user is logged

佐手、 提交于 2019-12-17 08:32:06

问题


My app's main activity has a navigation drawer, instantiated in the XML in this way:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/application_drawer"
    android:background="@color/white"/>

Now, the menu entry for the navigation drawer is the following:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/login"
        android:icon="@drawable/ic_action_person"
        android:title="@string/login"/>
    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/settings"/>
    <item
        android:id="@+id/terms"
        android:icon="@drawable/ic_action_about"
        android:title="@string/terms_and_conditions_menu"/>
    <item
        android:id="@+id/about"
        android:icon="@drawable/ic_action_about"
        android:title="@string/info_hotelsclick"/>
</group>

What I'd like to do is to change the first item (and possibly the others as well) dynamically under some conditions. For instance, I'd like to change the "Login" entry with a "logout" one once the user has logged in ;-)

How can I achieve that? I managed to add an item to the Drawer in this way

    Menu menu = navigationView.getMenu();
    menu.add("Test");

but it doesn't sound that good to me, I'm pretty sure there must be a cleaner way.

...but does it?


回答1:


I thing the best approach to this is to include all your items in the menu and the change their visibility.

<item
    android:id="@+id/login"
    android:icon="@drawable/ic_action_person"
    android:title="@string/login"
    android:visible="true" />

<item
    android:id="@+id/logout"
    android:icon="@drawable/ic_action_person"
    android:title="@string/logout"
    android:visible="false" />

then

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

You can also do this with whole groups of items

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

and

navigationView.getMenu().setGroupVisible(R.id.group_1, true)



回答2:


Simple solution:
Add two xml files in menu directory:

  1. navigation_with_login.xml Navigation Menu for logged in users

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_camera"
                android:icon="@drawable/ic_menu_camera"
                android:title="Import" />
            <item
                android:id="@+id/nav_gallery"
                android:icon="@drawable/ic_menu_gallery"
                android:title="Gallery" />
            <item
                android:id="@+id/nav_slideshow"
                android:icon="@drawable/ic_menu_slideshow"
                android:title="Slideshow" />
            <item
                android:id="@+id/nav_login"
                android:icon="@drawable/ic_menu_login"
                android:title="Login" />
        </group>
    
    
    </menu>
    
  2. navigation_with_logout.xml Navigation Menu for default user:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_camera"
                android:icon="@drawable/ic_menu_camera"
                android:title="Import" />
            <item
                android:id="@+id/nav_gallery"
                android:icon="@drawable/ic_menu_gallery"
                android:title="Gallery" />
            <item
                android:id="@+id/nav_slideshow"
                android:icon="@drawable/ic_menu_slideshow"
                android:title="Slideshow" />
            <item
                android:id="@+id/nav_logout"
                android:icon="@drawable/ic_menu_logout"
                android:title="Logout" />
        </group>
    
    </menu>
    

Now you can change NavigationView items,just write few lines of code.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

if(islogin)
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.navigation_with_login);
    } else
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.navigation_with_logout);
    }



回答3:


Frst get the navigationmenu

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();

To add the menu dynamically

 if(loggedOut){
        menu.add(R.id.submenu_others, R.id.action_logout, Menu.NONE, "logout");
}

Here is menu.add(groupId, menuItemId, orderOfMenu, menuItem text)

     if(loggedIn){
        menu.removeItem(R.id.action_logout);
}



回答4:


I got it done, try this when you need to change the title:

navigationView.getMenu().findItem(R.id.yourItemId).setTitle("my title");

Hope it helped!



来源:https://stackoverflow.com/questions/30918026/change-navigationview-items-when-user-is-logged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!