简体   繁体   中英

Android reload Navigation Drawer fragment

There is Login option in my navigation drawer. When logged in I want it to show Logout. Navigation drawer items also have drawable left to them. Is there any I can reload/refresh/reinitialize navigation drawer once Login has been done?

You can define a Layout for your navigation area like this:

<android.support.v4.widget.DrawerLayout>
<RelativeLayout>
   ....
    <ListView
       ...
        android:id="@+id/navigation_list"/>
    </ListView>
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>

From within your code you can now work with an Adapter and fill the content of your list as you wish, update works with notifyDataSetChanged . For each entry of your List you can define a specific layout (with or without buttons, etc.)

after once successful login you can get the view from adapter.

get that imageview or any other view change the image

ImageView imageView=(ImageView)getViewByPosition(pos,listview);
imageView.setImageResource(R.drawable.new_image);


public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

You can make two methods for loading data into navigation drawer listview one for login and one for logout and whenever you are creating activity check your session and load listview according to that. Similarly when you are logged in then you can call the logout method from another activity and refresh the naviagtion drawer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM