在我的应用程序中,我正在使用以下.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>

最佳答案

这是添加项目的代码。

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


请注意,子菜单扩展了菜单。我建议您检查MenuMenuItem的文档。

编辑

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

这样,您可以使用自定义适配器并按所需方式管理ID(但是,最佳做法是在RecyclerView(和旧版ListView)中使用长ID)。

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

10-06 13:56
查看更多