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>
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With