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>
navigationView.getMenu().getItem(pos).setChecked(true)
in drawer listener works fine, but not working in other places. – CryptoclasticnavigationView.setCheckedItem(R.id.xxx);
after delay 2s, it's working. But i can't figure out why and how to solve it. – CryptoclasticnavigationView.post()
. Thank you very much :-) – Cryptoclastic