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

查看:125
本文介绍了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天全站免登陆