Android 支持:设计 NavigationView 选中的菜单子项 [英] Android support:design NavigationView checked menu sub items

查看:24
本文介绍了Android 支持:设计 NavigationView 选中的菜单子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始将我的 android 应用程序转换为使用名为 support:design 的最新支持库.

I have recently started converting my android app to use the latest support library called support:design.

在实施新的 NavigationView 时,我偶然发现了显示所选菜单项的问题.

While implementing the new NavigationView i've stumbled upon a problem displaying the selected menu items.

我的 navdrawer_menu.xml

My navdrawer_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_home"
        android:icon="@drawable/ic_home_black"
        android:title="@string/navdrawer_item_home" />
</group>

    <item
        android:id="@+id/navigation_subheader"
        android:title="@string/navdrawer_subheader_title1">
        <menu>
            <group android:checkableBehavior="single">
            <item
                android:id="@+id/navigation_sub_item1"
                android:icon="@drawable/ic_home_black"
                android:title="@string/navdrawer_sub_item1" />
            </group>
        </menu>
    </item>
</menu>

接下来我将菜单项设置为在我的 onNavigationItemSelected 中签入:

Next I set the menu's item to checked in my onNavigationItemSelected:

@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {

    menuItem.setChecked(true);

    drawerLayout.closeDrawer(GravityCompat.START);
    mDrawerActionHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            displayView(menuItem.getItemId());
        }
    }, DRAWER_CLOSE_DELAY_MS);
    return true;
}

如果我只在标签之间使用普通菜单项,这很有效,但对于副标题则效果不佳.单击子项目不会将它们设置为选中状态,直到我单击同一项目两次,并且不会取消选中之前检查过的任何项目.

This works great if I only use normal menu items between the tags but it does not work very well for subheaders. Clicking on sub items wont set them checked untill i've clicked the same item twice and it won't uncheck the any item that checked previously.

最终看起来像这样:

推荐答案

每个项目都必须在一个组内,这样组才能在用户选择时控制项目的视觉行为.试试看:

Every item must be inside a group, so the group can control the item's visual behavor on user select. Try it:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_home"
        android:icon="@drawable/ic_home_black"
        android:title="@string/navdrawer_item_home" />
    <item
        android:id="@+id/navigation_subheader"
        android:title="@string/navdrawer_subheader_title1">
        <menu>
            <group android:checkableBehavior="single">
            <item
                android:id="@+id/navigation_sub_item1"
                android:icon="@drawable/ic_home_black"
                android:title="@string/navdrawer_sub_item1" />
            </group>
        </menu>
    </item>
</group>
</menu>

这篇关于Android 支持:设计 NavigationView 选中的菜单子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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