I'm trying to add submenu
items to NavigationView
programmatically . I'm able to add items into menu
but not into submenu
Adding items to menu works
Menu menu = mNavigationView.getMenu();
menu.add(Menu.NONE, Menu.NONE, index, "Menu Item1");
But adding items to sub menu doesn't work
Menu menu = mNavigationView.getMenu();
SubMenu subMenu = menu.addSubMenu("Sub menu title");
subMenu.add(Menu.NONE, Menu.NONE, index, "SubMenu Item1");
The trick to call
BaseAdapter.notifyDataSetChanged
on the underlyingAdapter
that contains the menu items. You could use reflection to grab theListView
or just loop over theNavigationView
children until you reach it.This isn't the most up-to-date code, as fas as I know Google hasn't pushed the most recent changes to the Support Library, but essentially
NavigationMenuPresenter.prepareMenuItems
is called when you callBaseAdpater.notifyDataSetChanged
.But if you want to see the most recent source, you can download it through the SDK Manager. Choose Sources for Android MNC. Then navigate to
Results
Somebody figured out a way to do it via reflection and accessing a private field. It's not pretty, but it'll work, for the moment. https://stackoverflow.com/a/30604299/4232051