Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

navigation drawer - How to programmatically add a submenu item to the new material design android support library

I am trying to add new menu items to a submenu in the new Material Design drawer panel.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

...

    <android.support.design.widget.NavigationView
        android:id="@+id/main_navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/channel_list_header"
        app:menu="@menu/menu_nav" /> 

</android.support.v4.widget.DrawerLayout>

I do have the @menu/menu_nav setup but now want to add new menu items:

NavigationView mDrawerList = (NavigationView) findViewById(R.id.main_navigation);

Menu menu = mDrawerList.getMenu();
Menu topChannelMenu = menu.addSubMenu("Top Channels");

topChannelMenu.add("Foo");
topChannelMenu.add("Bar");
topChannelMenu.add("Baz");

mDrawerList.invalidate(); // Trying to force a redraw, doesn't help.

The menu items from the @menu/menu_nav file are shown, but the newly added ones aren't. Any ideas how to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

[Update 20-03-2016] Bug is resolved. So no need to worry.

[This below contetnt is outdated.]

Adding a dynamic menu to NavigationView is currently bug on Design Support library. And I have report it to android bug source tracking. So wait till the bug will fixed. But if you want the temporary solution you can do it. First add your dynamic menu ..

    navView = (NavigationView) findViewById(R.id.navView);
    Menu m = navView.getMenu();
    SubMenu topChannelMenu = m.addSubMenu("Top Channels");
    topChannelMenu.add("Foo");
    topChannelMenu.add("Bar");
    topChannelMenu.add("Baz");

After adding your menu just write below code ..

    MenuItem mi = m.getItem(m.size()-1);
    mi.setTitle(mi.getTitle());

It's currently hacky solution. But work for me ...

[update 26-06-2015]

As I have reported this bug at Android Bug source Now Bug is marked as a future release, here is the link https://code.google.com/p/android/issues/detail?id=176300

So we can say that the bug is no more exist on future library. So you don't have to use tricky solution. i will also update this answer again when Future version release number is maintion for this bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...