我在我的应用中使用了一个带有两个菜单和两个headerView的navigationView。 当用户输入应用程序时,navigationView会显示第一组菜单+标题。当用户登录时,我会膨胀第二组。当用户注销时,我将再次显示第一组。
我一直在寻找,我已经设置了以下代码。在阶段我没有问题:“在应用程序中输入登录”然而,当我登录时,导航视图仍然在新标题上显示前一个标题。我对菜单没有任何问题。 我希望你能帮助我,谢谢你的时间!
public class MainActivity : ActivityBase
{
private Android.Support.V7.Widget.Toolbar toolbar;
private DrawerLayout drawerLayout;
private FrameLayout contentLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;
private bool isAuthenticated = true; //******
public MainActivity() : base(Resource.Layout.activity_main) { }
#region Methods
protected override void InitViews()
{
Typeface fontArialFont = Typeface.CreateFromAsset(Assets, AppInfo.ArialFontPath);
toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);
navigationView = FindViewById<NavigationView>(Resource.Id.navigationView);
contentLayout = FindViewById<FrameLayout>(Resource.Id.contentLayout);
// Set the toolbar and drawerLayout
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
drawerLayout.AddDrawerListener(actionBarDrawerToggle);
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayShowTitleEnabled(false);
actionBarDrawerToggle.SyncState();
// Set the NavigationView
LoadNavigationView(isAuthenticated);
navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
//PrepareDrawer();
//Load FragmentMain
LoadFragmentToActivity(new MainFragment(), null);
}
private void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_sign_in):
isAuthenticated = true;
LoadNavigationView(isAuthenticated);
break;
case (Resource.Id.nav_sign_out):
Toast.MakeText(this, "Sign out selected!", ToastLength.Short).Show();
isAuthenticated = false;
LoadNavigationView(isAuthenticated);
break;
}
//close drawer
drawerLayout.CloseDrawers();
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (actionBarDrawerToggle.OnOptionsItemSelected(item))
return true;
return base.OnOptionsItemSelected(item);
}
public void LoadNavigationView(bool isAuthenticated)
{
RelativeLayout afterloginHeaderView = FindViewById<RelativeLayout>(Resource.Id.afterloginHeaderView);
RelativeLayout preloginHeaderView = FindViewById<RelativeLayout>(Resource.Id.preloginHeaderView);
if (isAuthenticated)
{
navigationView.RemoveHeaderView(preloginHeaderView);
navigationView.InflateHeaderView(Resource.Layout.layout_nav_header_afterlogin);
navigationView.Menu.Clear();
navigationView.InflateMenu(Resource.Menu.menu_afterlogin);
}
else
{
navigationView.RemoveHeaderView(afterloginHeaderView);
navigationView.InflateHeaderView(Resource.Layout.layout_nav_header_prelogin);
navigationView.Menu.Clear();
navigationView.InflateMenu(Resource.Menu.menu_prelogin);
}
}
}
}
答案 0 :(得分:0)
我设法以这种方式修复了这个问题。
public void LoadNavigationView(bool isAuthenticated)
{
RelativeLayout afterloginHeaderView = FindViewById<RelativeLayout>(Resource.Id.afterloginHeaderView);
RelativeLayout preloginHeaderView = FindViewById<RelativeLayout>(Resource.Id.preloginHeaderView);
if (isAuthenticated)
{
navigationView.RemoveHeaderView(preloginHeaderView);
navigationView.InflateHeaderView(Resource.Layout.layout_nav_header_afterlogin);
navigationView.Menu.Clear();
navigationView.InflateMenu(Resource.Menu.menu_afterlogin);
}
else
{
View headerView = navigationView.GetHeaderView(0);
navigationView.RemoveHeaderView(headerView);
navigationView.InflateHeaderView(Resource.Layout.layout_nav_header_prelogin);
navigationView.Menu.Clear();
navigationView.InflateMenu(Resource.Menu.menu_prelogin);
}