Navigation drawer listview position not refreshing

拜拜、爱过 提交于 2019-12-02 07:09:30

You are always calling displayView() with the selected index position in the list of NavDrawerItems. In the case where the user is logged out, there are only 2 items in the list. When the user chooses "login", the index position is 1, not 5.

You probably want to add one more variable to NavDrawerItem which represents which Fragment to show when the user selects that item. You could use either an int value which is then passed to displayView(), or your could use the class name of the Fragment to show.

EDIT: Add example code

In NavDrawerItem, you should have something like:

String title;
Icon icon;

add the following:

int thingToDo;

Then, in the constructor of NavDrawerItem, add an int parameter to the constructor to initialize the field, like this:

public NavDrawerItem(String title, Icon icon, int thingToDo) {
    this.title = title;
    this.icon = icon;
    this.thingToDo = thingToDo;
}

change all your calls that create new NavDrawerItem instances to include the "thing to do", like this:

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1), 0));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), 5));

Then, in SlideMenuClickListener.onItemClick() method, do this:

public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
    // Get the item that was selected
    NavDrawerItem item = navDrawerItems.get(position);
    // display view for selected nav drawer item
    displayView(item.thingToDo);
}

You can do it in onDrawerOpened method like this.

  mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
    R.drawable.ic_drawer, //nav menu toggle icon
    R.string.app_name, // nav drawer open - description for accessibility
    R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);

            // get value from preference, whether user is logged in or not
            boolean isUserLoggedOut = getPrefBoolean("pref_is_user_logged_out",false);

            // Check Condition and Update list as required
            if(isUserLoggedOut){
                  navDrawerItems.clear();

                  // Home
                  navDrawerItems.add(new NavDrawerItem("Home", navMenuIcons.getResourceId(HOME_ICON_ID, -1)));
                 // Login
                 navDrawerItems.add(new NavDrawerItem("Login", navMenuIcons.getResourceId(LOGIN_ICON_ID, -1)));
            }else{
                  navDrawerItems.clear();

                  navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
                  // Find People
                  navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
                  // Photos
                  navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
                  // Communities, Will add a counter here
                  navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
                  // Pages
                  navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));

            }

            adapter.notifyDataSetChanged();

            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

The code is very confusing to follow. So I'll submit suggested sample code and at best it will be a workaround.

In method displayView(), it displays the Fragment based on position, obviously. When the app is in Login state, you only have 2 positions, Home and Login, as you stated. But displayView() will show the FindPeopleFragment when user selects Login, which is position = 1. Understand? This part of the code design is confusing, the code changes the positions dynamically on run-time while displayView() is static when checking for the position. Now, there are many solutions to this, I would suggest a minor code redesign such that the app checks for item strings instead of position.

However since that would be more involved, I can give you simple workaround fix. Code fix:

// Get the item that was selected
NavDrawerItem item = navDrawerItems.get(position);

if(position == 4) { // position of Sign out button
   displayView( 4 );   // show the Login fragment
   ...
}
else {
   displayView(item.thingToDo);
   ...
}

Note:

  1. I added the workaround fix of displayView( 4 );
  2. David Wasser put a good posted answer for you to understand. His post is useful and compliments my post well. I don't want to repeat his texts.

I need to move on with other questions. Good luck in your project...

If you detect login situation, you can set adapter of drawer If you are login change the navDrawerItems and set adapter.

adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);

    mDrawerList.setAdapter(adapter);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!