有什么方法可以为Drawer OnClose设置侦听器吗? (Xamarin)

时间:2019-05-01 23:09:06

标签: c# android xamarin toggle drawer

我在网络上看到了一些与Java有关的答案。

我不知道如何覆盖ActionBarDrawerToggle.onDrawerOpened(...)

有什么想法吗?

示例:12

1 个答案:

答案 0 :(得分:0)

首先,实现接口DrawerLayout.IDrawerListener

private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
private NavigationView navigationView;

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        Android.Support.V7.Widget.Toolbar toolbar =                 FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);


        drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        toggle = new ActionBarDrawerToggle(this, drawer,         Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
        drawer.AddDrawerListener(toggle);
        drawer.AddDrawerListener(this);
        toggle.SyncState();
    }

现在在活动中实现功能:

public void OnDrawerClosed(View drawerView)
        {
            //Code here
            ((DrawerLayout.IDrawerListener)toggle).OnDrawerClosed(drawerView);
        }

        public void OnDrawerOpened(View drawerView)
        {
            //Code here
            ((DrawerLayout.IDrawerListener)toggle).OnDrawerOpened(drawerView);
        }

        public void OnDrawerSlide(View drawerView, float slideOffset)
        {
            //Code here
            ((DrawerLayout.IDrawerListener)toggle).OnDrawerSlide(drawerView, slideOffset);
        }

        public void OnDrawerStateChanged(int newState)
        {
            //Code here
((DrawerLayout.IDrawerListener)toggle).OnDrawerStateChanged(newState);
        }

注意:代码不完整,所以请告诉我是否有任何问题。