MvvmCross - Soft back button does not work when using DrawerLayout

送分小仙女□ 提交于 2019-12-25 01:33:42

问题


I clone Playground Android example in MvvmCross and set DrawerLayout by default.

RegisterAppStart<SplitRootViewModel>();

When navigated to SplitDetailNavView from SplitDetailView, there is no back button on navigation bar. I had to modify OnCreateView function in SplitDetailNavView

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
     base.OnCreateView(inflater, container, savedInstanceState);

     var view = this.BindingInflate(Resource.Layout.SplitDetailView, null);
_toolbar = view.FindViewById<Toolbar>(Resource.Id.toolbar);
     if (_toolbar != null) {
         BaseActivity.SetSupportActionBar(_toolbar);
         BaseActivity.SupportActionBar.SetDisplayHomeAsUpEnabled(true);
     }
     return view;
 }

Then, back buttons appears but doesn't work. Hard button still works as expect.

Any suggestion? Github for anyone interested DrawerLayout for both Android and iOS https://github.com/closetoyou293/HambugerSample


回答1:


Thanks @Pietv, you can refer to this.

In your BaseFragment's OnCreateView method, add this:

 Toolbar.NavigationClick += Toolbar_NavigationClick;

Toolbar_NavigationClick:

private void Toolbar_NavigationClick(object sender, Android.Support.V7.Widget.Toolbar.NavigationClickEventArgs e)
{
    var mainActivity = Activity as MainActivity;
    if (!mainActivity.Title.Equals("Main Activity"))
    {
        mainActivity?.OnBackPressed();
    }
    else {
        mainActivity.ShowDrawLayout();
    }
}

Add ShowDrawLayout method in MainActivity:

public void ShowDrawLayout() {
    DrawerLayout.OpenDrawer(GravityCompat.Start);
}

Your BaseFragment's OnCreateView method should like this:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignore = base.OnCreateView(inflater, container, savedInstanceState);

    var view = this.BindingInflate(FragmentId, null);

    Toolbar = view.FindViewById<Toolbar>(Resource.Id.toolbar);
    if (Toolbar != null)
    {
        var mainActivity = Activity as MainActivity;
        if (mainActivity == null) return view;

        mainActivity.SetSupportActionBar(Toolbar);
        mainActivity.SupportActionBar?.SetDisplayHomeAsUpEnabled(true);
        DrawerToggle = new MvxActionBarDrawerToggle(
            Activity,                               // host Activity
            mainActivity.DrawerLayout,  // DrawerLayout object
            Toolbar,                               // nav drawer icon to replace 'Up' caret
            Resource.String.drawer_open,            // "open drawer" description
            Resource.String.drawer_close            // "close drawer" description
        );

        if (ShowHamburgerMenu)
        {
            DrawerToggle.DrawerOpened += (sender, e) =>
            {
                mainActivity?.HideSoftKeyboard();
            };
            mainActivity.DrawerLayout.AddDrawerListener(DrawerToggle);
        }

        DrawerToggle.DrawerIndicatorEnabled = ShowHamburgerMenu;
        Toolbar.NavigationClick += Toolbar_NavigationClick;
    }
    return view;
}


来源:https://stackoverflow.com/questions/49621880/mvvmcross-soft-back-button-does-not-work-when-using-drawerlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!