问题
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