如何在Google的新NavigationView中将MenuItem添加到SubMenu? [英] How to add MenuItem to SubMenu in Google's new NavigationView?

查看:84
本文介绍了如何在Google的新NavigationView中将MenuItem添加到SubMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为抽屉菜单制作了一个xml,其中包含以下MenuItem:

I have made an xml for my drawer menu which contains the following MenuItem:

<item android:title="Contacts"
    android:id="@+id/contact_list">
  <menu>

  </menu>
</item>

在MainActivity中,我获得了对空SubMenu的引用,并尝试将新的测试MenuItem添加到SubMenu.

In my MainActivity I get a reference to the empty SubMenu and try to add a new test MenuItem to the the SubMenu.

mNavMenu = mNavigationView.getMenu();
mNavMenu.findItem(R.id.contact_list).getSubMenu().add("hello");

不幸的是,这不起作用,并且没有添加MenuItems.我尝试了许多其他不同的变体,但是没有一个更新我的导航抽屉中的SubMenu.

Unfortunately, this is not working and the MenuItems are not being added. I have tried many other different variations of this but none of them update the SubMenu in my navigation drawer.

这可能是一个错误吗?

推荐答案

因此,显然,目前唯一的解决方案是使用反射来调用菜单上的notifyDataSetChanged():

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.

这篇关于如何在Google的新NavigationView中将MenuItem添加到SubMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆