导航抽屉如何突出显示所选项目

时间:2016-06-11 19:43:20

标签: android navigation menuitem highlighting drawer

我在我的应用程序中使用导航抽屉,你可以在android工作室添加,我在Nav抽屉中添加了一些菜单条目(参见代码块)。如果我按下第一个项目(nav_home)或第二个项目,导航抽屉会突出显示当前按下的项目。

如果我按下“nav_information”按钮或其他项目,则会显示新的(单击的)片段opend并且不会突出显示(主页或第二项仍然突出显示)

很快,只有第一项显示所选项目的突出显示。

  @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    Fragment myFragment = null;

    if (id == R.id.nav_home) {
            drawer.setSelected(true);
        myFragment = new MainScreen();
    }else if (id == R.id.nav_greetings){
        myFragment = new Greetings();
    } else if (id == R.id.nav_information) {
        myFragment = new Aktuelles();
    } else if (id == R.id.nav_directions) {
        myFragment = new Anfahrt();
    } else if (id == R.id.nav_basilika) {
        myFragment = new Rundabout();
    } else if (id == R.id.nav_download) {
        myFragment = new Downloads();
    } else if (id == R.id.nav_history) {
        myFragment = new History();
    }else if(id == R.id.nav_hauptwallfahrt){
        myFragment = new Hauptwallfahrt();
    }else if (id == R.id.nav_themenwallfahrt){
        myFragment = new Themenwallfahrt();
    }else if (id == R.id.nav_pilgergruppen){
        myFragment = new Pilgergruppen();
    }else if (id ==R.id.nav_wallfahrtgodi){
        myFragment = new Wallfahrtsgottesdienste();
    }


    FragmentManager fragmentManager = getSupportFragmentManager();

    FragmentTransaction ft = fragmentManager.beginTransaction();

    // Replace the requestet Fragment
    ft.replace(R.id.container, myFragment) ;
    ft.commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

这就是我处理片段打开的方式。

{{1}}

有没有办法“启用”突出显示?

提前致谢!

3 个答案:

答案 0 :(得分:4)

你只需要改变标签的顺序

我尝试了最后一组,但它确实有效,请检查

required

答案 1 :(得分:0)

                      

</group>
<item android:title="Neuigkeiten">
    <menu>
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_lang"
                android:icon="@drawable/lang_blue"
                android:title="@string/language" />
            <item
                android:id="@+id/nav_invite"
                android:icon="@drawable/call_friend_blue"
                android:title="@string/invite_friend" />
            <item
                android:id="@+id/nav_about"
                android:icon="@drawable/about_us_blue"
                android:title="@string/about_health_in_touch" />
        </group>
    </menu>
</item>

答案 2 :(得分:0)

以这种方式完成:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/home_blue"
            android:title="@string/home" />
        <item
            android:id="@+id/nav_search"
            android:icon="@drawable/search_icon"
            android:title="@string/search" />

    </group>
    <item android:title="Neuigkeiten">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_lang"
                    android:icon="@drawable/lang_blue"
                    android:title="@string/language" />
                <item
                    android:id="@+id/nav_invite"
                    android:icon="@drawable/call_friend_blue"
                    android:title="@string/invite_friend" />
                <item
                    android:id="@+id/nav_about"
                    android:icon="@drawable/about_us_blue"
                    android:title="@string/about_health_in_touch" />
            </group>
        </menu>
    </item>
</menu>