本文介绍了MvvmCross-使用DrawerLayout时,软后退按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MvvmCross中克隆了Playground Android示例,并默认设置了DrawerLayout.

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

RegisterAppStart<SplitRootViewModel>();

从SplitDetailView导航到SplitDetailNavView时,导航栏上没有后退按钮.我不得不在SplitDetailNavView中修改OnCreateView函数

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.

有什么建议吗?适用于Android和iOS的DrawerLayout的任何人的Github https://github.com/closetoyou293/HambugerSample

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

推荐答案

感谢@Pietv,您可以参考.

Thanks @Pietv, you can refer to this.

在您的BaseFragmentOnCreateView方法中,添加以下内容:

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

MainActivity中添加ShowDrawLayout方法:

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


您的BaseFragmentOnCreateView方法应如下所示:


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

这篇关于MvvmCross-使用DrawerLayout时,软后退按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 21:19