How to add MenuItem to SubMenu in Google's new NavigationView?

喜夏-厌秋 提交于 2019-12-01 01:14:08

So apparently, the only solution right now is to use reflection to call notifyDataSetChanged() on the menu:

    for (int j = 0, count = mNavigationView.getChildCount(); j < count; j++) {
      final View child = mNavigationView.getChildAt(j);
      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();
      }
     }

This will update the menu with your new items.

You are doing it in wrong way... You don't have to find the menu agian ... just call the method on the instance of mNavMenu ..

 mNavMenu = mNavigationView.getMenu();
 mNavMenu.addSubMenu("It's new bro .. ");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!