Cannot catch toolbar home button click event

前端 未结 11 2006
悲&欢浪女
悲&欢浪女 2020-11-27 02:56

I\'ve implemented the newest appcompat library and using the Toolbar as action bar. But the problem is I cannot catch the home button / hamburger icon click eve

11条回答
  •  长情又很酷
    2020-11-27 03:21

    For anyone looking for a Xamarin implementation (since events are done differently in C#), I simply created this NavClickHandler class as follows:

    public class NavClickHandler : Java.Lang.Object, View.IOnClickListener
    {
        private Activity mActivity;
        public NavClickHandler(Activity activity)
        {
            this.mActivity = activity;
        }
        public void OnClick(View v)
        {
            DrawerLayout drawer = (DrawerLayout)mActivity.FindViewById(Resource.Id.drawer_layout);
            if (drawer.IsDrawerOpen(GravityCompat.Start))
            {
                drawer.CloseDrawer(GravityCompat.Start);
            }
            else
            {
                drawer.OpenDrawer(GravityCompat.Start);
            }
        }
    }
    

    Then, assigned a custom hamburger menu button like this:

            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDefaultDisplayHomeAsUpEnabled(false);
            this.drawerToggle.DrawerIndicatorEnabled = false;
            this.drawerToggle.SetHomeAsUpIndicator(Resource.Drawable.MenuButton);
    

    And finally, assigned the drawer menu toggler a ToolbarNavigationClickListener of the class type I created earlier:

            this.drawerToggle.ToolbarNavigationClickListener = new NavClickHandler(this);
    

    And then you've got a custom menu button, with click events handled.

提交回复
热议问题