简体   繁体   English

Android重新加载导航抽屉片段

[英]Android reload Navigation Drawer fragment

There is Login option in my navigation drawer. 导航抽屉中有登录选项。 When logged in I want it to show Logout. 登录后我希望它显示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 . 在您的代码中,您现在可以使用Adapter并根据需要填充列表的内容,更新适用于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=(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. 同样,当您登录时,您可以从另一个活动调用logout方法并刷新naviagtion抽屉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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