Android支持:设计NavigationView选中的菜单子项

时间:2015-06-11 11:36:22

标签: menu navigation menuitem submenu

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

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

我的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:

@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;
}

如果我只在标签之间使用普通菜单项,但这对子标题不起作用,那么效果很好。点击子项目不会设置它们检查,直到我点击相同的项目两次,它不会取消选中之前检查的任何项目。

最终看起来像这样:

enter image description here

4 个答案:

答案 0 :(得分:7)

每个项目必须在一个组内,因此该组可以控制该项目在用户选择时的视觉行为。试试吧:

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

答案 1 :(得分:3)

I resolved this recurrent problem in this way that works very well.

We must simply memorize the id of the selected item, recharge your menu on your NavigationView and select the item again.

For that you need to have your menu drawer like way :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/section_1"
        android:title="@string/section_title">

        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/item_1_section_1"
                    android:icon="@drawable/ic_icon"
                    android:title="@string/title"/>

                <item
                    android:id="@+id/item_2_section_1"
                    android:icon="@drawable/ic_icon"
                    android:title="@string/title"/>
                ...
            </group>
        </menu>
    </item>
        ...
    <item
        android:id="@+id/section_x"
        android:title="@string/section_title">

        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/item_1_section_x"
                    android:icon="@drawable/ic_icon"
                    android:title="@string/title"/>

                <item
                    android:id="@+id/item_2_section_x"
                    android:icon="@drawable/ic_icon"
                    android:title="@string/title"/>
                ...
            </group>
        </menu>
    </item>
</menu>

So, my solution :

public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

...
private NavigationView mNavigationView;
private int mNavItemId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...

    // load saved navigation state if present
    if (null == savedInstanceState) {
        mNavItemId = R.id.item_1_section_1;
    } else {
        mNavItemId = savedInstanceState.getInt(NAV_ITEM_ID);
    }

    // listen for navigation events
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(this);

    // select the correct nav menu item
    mNavigationView.getMenu().findItem(mNavItemId).setChecked(true);
}

@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
    // update highlighted item in the navigation menu
    mNavItemId = menuItem.getItemId();

    // allow some time after closing the drawer before performing real navigation
    // so the user can see what is happening
    mDrawerLayout.closeDrawer(GravityCompat.START);
    mDrawerActionHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            ...
        }
    }, DRAWER_CLOSE_DELAY_MS);

    // Reload menuDrawer to keep selected item
    mNavigationView.getMenu().clear();
    mNavigationView.inflateMenu(R.menu.menu_drawer);
    mNavigationView.getMenu().findItem(mNavItemId).setChecked(true);

    return true;
}

...

}

答案 2 :(得分:3)

很简单。 只需为每个项目添加 android:checkable =&#34; true&#34;

例如。

<item
        android:id="@+id/navigation_item_home"
        android:icon="@drawable/ic_home_black"
        android:title="@string/navdrawer_item_home"
        android:checkable="true" />

答案 3 :(得分:1)

我建议采用@MichelFortes提供的解决方案,因为它解决了我在NavigationView中使用子菜单项的问题。这是我的代码,它可以正常工作

<group android:checkableBehavior="single">
    <item
        android:id="@+id/your_id_item"
        android:icon="@drawable/your_icon_if_you_want"
        android:title="@string/title_for_this_item" />

   <!-- you can add here as many items as you want -->

</group>

<item
    android:title="@string/submenu_title">
    <menu>
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/submenu_item_id"
                android:icon="@drawable/icon_if_exists"
                android:title="@string/title_for_item" />

          <!-- you can add here as many items as you want -->

        </group>
    </menu>
</item>

在这里,您选择的每个项目都会被选中并突出显示! 再次感谢@MichelFortes;)