运行时导航抽屉项目

Runtime navigation drawer items

在我的应用程序中,我使用以下内容加载导航抽屉。xml。在第一组中,我有 current_deviceother_device 的项目。此处可能会列出其他几个设备,但这是在运行时通过 api 调用确定的。有没有办法动态地向这个 .xml 添加项目?或者更好的方法来做到这一点。 c

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group
    android:id="@+id/nav_devices_group"
    android:checkableBehavior="single">

    <item
        android:id="@+id/nav_current_device"
        android:icon="@drawable/common_full_open_on_phone"
        android:title="@string/nav_current_device" />

    <item
        android:id="@+id/nav_other_device"
        android:icon="@drawable/common_full_open_on_phone"
        android:title="@string/nav_devices" />

</group>

<group>
    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_setting_dark"
        android:title="@string/nav_settings" />

    <item
        android:id="@+id/nav_about"
        android:icon="@android:drawable/ic_dialog_info"
        android:title="@string/nav_about" />
</group>

</menu>

导航抽屉布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/menu_nav_drawer" />

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

发件人:

向 NavigationView 添加动态菜单目前是设计支持库中的错误我已将其报告给 android 错误源跟踪。所以等到错误修复。但如果你想要临时解决方案,你可以做到。首先添加你的动态菜单..

    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");

添加菜单后只需编写以下代码..

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

它目前是 hacky 解决方案。但是为我工作...

这是添加项目的代码。

mNavigationView = (NavigationView) context.findViewById(R.id.navigationView);
Menu menu = mNavigationView.getMenu();
SubMenu devicesMenu = menu.addSubMenu(Menu.NONE, DEVICES_MENU_ID, Menu.NONE, "Devices");
//You get your deviceId (nexus5Id, nexus6Id and so on) from your API call
//You should see deviceId in your code, from a loop or network callback
//in place of my hardcoded devices ids
devicesMenu.add(DEVICES_MENU_ID, nexus5Id, "Nexus 5");
devicesMenu.add(DEVICES_MENU_ID, nexus6Id, "Nexus 6");
devicesMenu.add(DEVICES_MENU_ID, nexus5XId, "Nexus 5X");
devicesMenu.add(DEVICES_MENU_ID, nexus6PId, "Nexus 6P");

要获取菜单项、将其设置为选中、更改其图标或其他任何内容:

MenuItem device = devicesMenu.find(deviceId);
devicesMenu.setChecked(true);

删除项目:

devicesMenu.remove(deviceId);

请注意,SubMenu 扩展了 Menu。我建议你查看 Menu and MenuItem.

的文档

编辑

由于您的 ID 不适合 MenuItems 要求的 int 格式,我认为您应该添加 ScrollView/NestedScrollView 代替 NavigationMenu,为非组项目添加带有 drawableLeft 的经典 TextView,以及可扩展视图 (例如 LinearLayout)和 TextView 组,单击时展开,显示包含所有设备的 RecyclerView。

通过这种方式,您可以使用自定义适配器并按照您想要的方式管理您的 ID(不过,最佳做法是在 RecyclerView(和旧版 ListView)中使用长 ID)。

但是,我不确定在 NavigationDrawer 中添加所有设备(无论是带菜单的标准 API,还是带 RecyclerView 的设备)是否是一个好习惯,因为设备可能很长,正确的?对于大型设备集合,我会在 NavigationDrawer 中使用标准的非分组 "Devices" 项,然后向用户显示搜索框或设备列表。