Search code examples
c#xamarin.androidnavigationview

NavigationView, replace headerView and Menu


I am using a navigationView in my app with two menu and two headerView. When the user enter in the app, the navigationView display the first set of menu+header. When the user loggin, then I will inflate the second set. When the user log out, i will display again the first set.

I have been searching for a while, and I have set the following code. I have no problem during the phase: "Entering in the App to SignIn" however, when i SignOut the navigationView still show the previous header upon the new header. I have no problem with the menu. I hope you can help me and thank you for your time!

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);

        }
    }
}

}


Solution

  • I managed to fix the issue in this way.

     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);
    
            }