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

一曲冷凌霜 提交于 2019-11-27 00:05:46

[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.

You can add and remove item from navigation view by changing the visibility. Get the item at index and setVisible to true to show and false to hide it.

MenuItem item = navigationView.getMenu().getItem(3);
item.setVisible(false);

I found easier way to change navigation view ( work with both submenu and menu). You can re-inflate NavigationViewat runtime with 2 lines of code. In this example, I re-inflate new_navigation_drawer_items.xml using public method inflateMenu (You can create a menu xml file with your new submenu)

navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.new_navigation_drawer_items); //inflate new items.

Use this method to add menu and submenu

private void addItemsRunTime(NavigationView navigationView) {

        //adding items run time
        final Menu menu = navigationView.getMenu();
        for (int i = 1; i <= 3; i++) {
            menu.add("Runtime item "+ i);
        }

        // adding a section and items into it
        final SubMenu subMenu = menu.addSubMenu("SubMenu Title");
        for (int i = 1; i <= 2; i++) {
            subMenu.add("SubMenu Item " + i);
        }

        // refreshing navigation drawer adapter
        for (int i = 0, count = mNavigationView.getChildCount(); i < count; i++) {
            final View child = mNavigationView.getChildAt(i);
            if (child != null && child instanceof ListView) {
                final ListView menuView = (ListView) child;
                final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter();
                final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter();
                wrapped.notifyDataSetChanged();
            }
        }
    }

and call this method where you setup Drawer Content.

This is how I manipulate the sub-menu item in NavigationView

1) Create menu.xml file

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <group android:checkableBehavior="single">
    <item android:title="Index:" android:id="@+id/mnugrp_Index">
      <menu>

        <!--This line below is dump line for rendering dynamic sub-menu item-->
        <item android:icon="@drawable/ic_action_content_create" android:title="Sub item 1" />

      </menu>
    </item>
  </group>

  <group android:id="@+id/mnugrp_Attachment" android:checkableBehavior="single">
    <item android:title="Attachment:" android:id="@+id/mnugrp_Attachment">
    <menu>
            
      <!--This line below is dump line for rendering dynamic sub-menu item-->
      <item android:icon="@drawable/ic_action_content_create" android:title="Sub item 1" />
    
    </menu>
  </item>
  </group>  
</menu>

2) Manipulate the menu by calling this function

        /// <summary>
    /// 
    /// </summary>
    /// <param name="navView"></param>
    private void AddNavigationMenuItem (NavigationView navView)
    {
        navView.Menu.FindItem(Resource.Id.mnugrp_Index).SubMenu.Clear();
        navView.Menu.FindItem(Resource.Id.mnugrp_Index).SubMenu.Add("This is Index 1");
        navView.Menu.FindItem(Resource.Id.mnugrp_Index).SubMenu.Add("This is Index 2");
        navView.Menu.FindItem(Resource.Id.mnugrp_Index).SubMenu.Add("This is Index 3");
        navView.Menu.FindItem(Resource.Id.mnugrp_Index).SubMenu.Add("This is Index 4");

        navView.Menu.FindItem(Resource.Id.mnugrp_Attachment).SubMenu.Clear();
        navView.Menu.FindItem(Resource.Id.mnugrp_Attachment).SubMenu.Add("This is Attachment 1");
        navView.Menu.FindItem(Resource.Id.mnugrp_Attachment).SubMenu.Add("This is Attachment 2");
        navView.Menu.FindItem(Resource.Id.mnugrp_Attachment).SubMenu.Add("This is Attachment 3");
        navView.Menu.FindItem(Resource.Id.mnugrp_Attachment).SubMenu.Add("This is Attachment 4");
    }

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