Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationView with DrawerLayout setCheckedItem not working

I want let NavigationView check one item when app startup. But i found NavigationView.setCheckedItem(R.id.xxx) not working. And i also tried navigationView.getMenu().findItem(R.id.xxx).setChecked(true), same result.

I've already set checkableBehavior to single.

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

                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/xxx" />

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

But there is one way worked:

drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            navigationView.setChecked(R.id.xxx);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
        }

        @Override
        public void onDrawerStateChanged(int newState) {
        }
    });

The navigation item would be checked in onDrawerOpened callback. I've searched a lot in stackoverflow but none methods work for me. Who can help me about this.

EDIT-1

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/xxx"
        android:icon="@drawable/xxx"
        android:title="@string/recent_display_name" />

    <item android:title="@string/sliding_menu_group_places">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/my_files_display_name" />

                <item
                    android:id="@+id/nav_item_sdcard"
                    android:icon="@drawable/ic_nav_sdcard"
                    android:title="@string/storage_display_name" />
            </group>
        </menu>
    </item>

    <item android:title="@string/sliding_menu_group_tool">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/clean_display_name" />
            </group>
        </menu>
    </item>

    <item android:title="@string/sliding_menu_group_settings">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/settings_display_name" />

                <item
                    android:id="@+id/xxx"
                    android:icon="@drawable/xxx"
                    android:title="@string/exit_display_name" />
            </group>

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

like image 251
Dennis Lu Avatar asked Dec 24 '22 00:12

Dennis Lu


1 Answers

Thanks to @darwin.

Reason: navigationView.setCheckedItem(R.id.xxx) not working because the NavigationView isn't ready for it.

Solution:

navigationView.post(new Runnable() {
        @Override
        public void run() {
            navigationView.setCheckedItem(id);
        }
    });
like image 186
Dennis Lu Avatar answered Dec 26 '22 16:12

Dennis Lu