How to use notifyDataSetChanged with new nav menu?

I am using the new NavigationView

one in one of my recent projects. However, I have a problem with update data.

I used ListView

to use in mine DrawerLayout

, and when I needed to change my data, I called the method of notifyDataSetChanged()

my adapter.

Currently NavigationView

not a method notifyDataSetChanged()

and when I want to update an item in my menu nothing happens, e.g .:

 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? Thank you for your help.

+3


source to share


3 answers


UPDATE: This was fixed in v.0.0.0, so you don't need to do anything yourself, just update your dependency. Got the answer: fooobar.com/questions/240398 / ...

Old solution (just to see how stupid it might be):

But I found a working solution in this fooobar.com/questions/240398 / ... answer . It uses reflection to call the updateMenuView (boolean) of the NavigationView presenter.



I changed the code from the answer for my purposes. Check also the answer method and choose which one is best 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();
    }
}

      

PS Interestingly, updateMenuView ignores the value given to it.

+2


source


Instead of referencing a group and then finding an element from that group and setting visibility, try referencing that element directly ...

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);

      



This works for me. Hope this helps you.

+1


source


@Moinkhan Thank you for your helper, but not working for me. Here's 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 did not update!

+1


source







All Articles