运行时导航抽屉项目

时间:2015-10-07 03:36:34

标签: 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>

2 个答案:

答案 0 :(得分:1)

以下是添加项目的代码。

$>history
... some command here
...
$>sh my_bash_file.sh
$>history
... some command here
...

要获取菜单项,要将其设置为已选中,请更改它的图标或任何内容:

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

请注意,SubMenu扩展了Menu。建议您查看MenuMenuItem的文档。

修改

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

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

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

答案 1 :(得分:0)

来自:How to programmatically add a submenu item to the new material design android support library

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

    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解决方案。但是为我工作......