android - 运行时抽屉导航项目

标签 android android-layout navigation-drawer xml-layout

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

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

请注意,SubMenu 扩展了 Menu。我建议你查看 Menu 的文档和 MenuItem .

编辑

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

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

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

关于android - 运行时抽屉导航项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983384/

相关文章:

android - manifest.xml 中的主题

android - 动态创建布局

带有抽屉 + 工具栏 + fragment 的 Android App 模板

android - 在按钮/图像上单击切换抽屉导航 'open'

android - 在 Android 模拟器中获取 x,y 位置的像素颜色

java - 如何在单击按钮时将数据写入特征?

android - ClassNotFoundException com.google.android.gms.measurement.AppMeasurement

android - 自动完成 TextView : AfterTextChange and OnItemClick working together

Android:以编程方式创建 TextView

android - 我可以从工具栏中删除汉堡包(抽屉导航图标),删除它的区域(矩形)吗?