Reputation: 715
I use the new NavigationView
in one of my recent projects. However I have a problem for the update data.
Previously, I used a ListView
in my DrawerLayout
and when I needed to change my data I called notifyDataSetChanged()
method of my Adapter.
Currently NavigationView
does not notifyDataSetChanged()
method and when I want to update an item on my menu nothing is happening, for example:
Menu menuAccount = navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu();
menuAccount.findItem(R.id.drawer_item_login).setVisible(!isLoggedIn);
Do you have a solution ? Thanks you for your help.
Upvotes: 3
Views: 1754
Reputation: 391
UPDATE: This was fixed in v23.0.0 so you don't need to do anything by youself, just update your dependency. Got from this answer: https://stackoverflow.com/a/32181083/2489474
Old solution (just to see how stupid it could be):
But I've found working solution in this answer https://stackoverflow.com/a/30604299/2489474. It uses reflection to call updateMenuView(boolean) of NavigationView's presenter.
I've modified code from the answer for my purposes. Check also a method from the answer and choose which one is better for you.
//HACK
private void updateNavigationView() {
try {
Field presenterField = NavigationView.class.getDeclaredField("mPresenter");
presenterField.setAccessible(true);
((NavigationMenuPresenter) presenterField.get(navigationView_)).updateMenuView(false);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
P.S. it is interesting that updateMenuView ignores a value was given to it.
Upvotes: 2
Reputation: 12932
Instead of referencing group and then finding a item from that group and setting it's visibility try to reference the item directly like this ...
navView.getMenu().findItem(R.id.drawer_item_login).setVisible(!isLoggedin);
navView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().setGroupVisible(R.id.group_actions_user, !isLoggedin);
It works for me. I hope it helps you.
Upvotes: 1
Reputation: 715
@Moinkhan Thanks you for your helper but doesn't work for me. Here my menu_drawer.xml
<group
android:checkableBehavior="single"
android:id="@+id/group1">
<item
android:id="@+id/drawer_item_publications_list"
android:icon="@drawable/ic_drawer_publications_24dp"
android:title="@string/drawer_menu_item_publications" />
</group>
<group android:id="@+id/drawer_group_account">
<item
android:title="@string/drawer_menu_sub_item_account"
android:id="@+id/drawer_item_account">
<menu>
<item
android:icon="@drawable/ic_drawer_login_24dp"
android:id="@+id/drawer_item_login"
android:title="@string/drawer_menu_item_login" />
<group
android:id="@+id/group_actions_user">
<item
android:icon="@drawable/ic_drawer_add_publication_24dp"
android:id="@+id/drawer_item_add_publication"
android:title="@string/drawer_menu_item_add_publication" />
<item
android:icon="@drawable/ic_drawer_my_publications_24dp"
android:id="@+id/drawer_item_my_publications"
android:title="@string/drawer_menu_item_my_publications" />
<item
android:icon="@drawable/ic_drawer_edit_profil_24dp"
android:id="@+id/drawer_item_edit_profil"
android:title="@string/drawer_menu_item_edit_profil" />
<item
android:icon="@drawable/ic_drawer_delete_account_24dp"
android:id="@+id/drawer_item_delete_account"
android:title="@string/drawer_menu_item_delete_account" />
<item
android:icon="@drawable/ic_drawer_logout_24dp"
android:id="@+id/drawer_item_logout"
android:title="@string/drawer_menu_item_logout" />
</group>
</menu>
</item>
</group>
And my method to update my NavigationView
private void setUpNavigationDrawer()
{
boolean isLoggedIn = sessionManager.isLoggedIn();
navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().findItem(R.id.drawer_item_login).setVisible(!isLoggedIn);
navigationView.getMenu().findItem(R.id.drawer_item_account).getSubMenu().setGroupVisible(R.id.group_actions_user, isLoggedIn);
}
After some operations I called setUpNavigationDrawer() but the menu was not updated !
Upvotes: 1