Change NavigationView items when user is logged

2019-01-10 11:14发布

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?

4条回答
看我几分像从前
2楼-- · 2019-01-10 11:16

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)
查看更多
疯言疯语
3楼-- · 2019-01-10 11:19

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);
    }
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-10 11:27

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!

查看更多
Melony?
5楼-- · 2019-01-10 11:29

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);
}
查看更多
登录 后发表回答