简体   繁体   English

Android中的导航抽屉重新加载问题

[英]Navigation drawer reload issue in android

I have a navigation drawer in my application. 我的应用程序中有一个导航抽屉。 I want to change navigation drawer layout on a button tap. 我想在按钮水龙头上更改导航抽屉布局。 but each time i toggle the layout, it is simply added to the existing navigation drawer(duplicate entry is showing), but i want to replace the whole view to the new one. 但是每次我切换布局时,它只是被添加到现有的导航抽屉中(显示重复的条目),但是我想将整个视图替换为新视图。

How can i implement this ? 我该如何执行呢?

Please refer my code 请参考我的代码

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    if (checkIsLoggedIn()){
        navigationView.inflateMenu(R.menu.activity_main_drawer_logged_in);
    }
    else{
        navigationView.inflateMenu(R.menu.activity_main_drawer_logged_out);
        addHeaderLayout(navigationView);
      }

Here i have inflated activity_main_drawer_logged_in and activity_main_drawer_logged_out in the code as per the status of checkIsLoggedIn(). 在这里,根据checkIsLoggedIn()的状态,代码中的activity_main_drawer_logged_in和activity_main_drawer_logged_out都已膨胀。

Issue: If I logout from the application while navigation drawer is showing the layout for login 问题:如果我在导航抽屉显示登录布局时从应用程序注销

Then the contents of both activity_main_drawer_logged_out and activity_main_drawer_logged_in layouts is showing in the navigation drawer. 然后,activity_main_drawer_logged_out和activity_main_drawer_logged_in布局的内容都显示在导航抽屉中。

Make these changes to your code 对您的代码进行这些更改

if (checkIsLoggedIn()){
    navigationView.getMenu().clear();
    navigationView.inflateMenu(R.menu.activity_main_drawer_logged_in);
}
else{
    navigationView.getMenu().clear();
    navigationView.inflateMenu(R.menu.activity_main_drawer_logged_out);
    addHeaderLayout(navigationView);
  }

I had a problem while using the above answer. 使用上述答案时出现问题。 Duplicate header view is showing 显示重复的标题视图

I found a better solution for navigation view header duplicate while add header view dynamically. 我发现了一个更好的解决方案,用于在导航视图标题重复的同时动态添加标题视图。 This is the code snippet. 这是代码片段。

if (navigationView != null) {
   for (int i = 0; i < navigationView.getHeaderCount(); i++) {
   if (navigationView.getHeaderView(i) != null)
   navigationView.getHeaderView(i).setVisibility(View.GONE);
   }
   addHeaderLayout(navigationView);
  }

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

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