Can I listen to when the Navigation drawer is opened or closed (e.g: listener like button onclick)?

前端 未结 4 1594
别跟我提以往
别跟我提以往 2021-01-12 02:38

I want to call some methods when Navigation Drawer is opened & closed. How can I listen to it (like button\'s onclick listener) ?

I know we can check (mDra

4条回答
  •  执笔经年
    2021-01-12 03:26

    Yeah there are four abstract methods that you can use as listeners

    public abstract void onDrawerClosed (View drawerView)
    public abstract void onDrawerOpened (View drawerView)
    public abstract void onDrawerSlide (View drawerView, float slideOffset)
    public abstract void onDrawerStateChanged (int newState)
    

    can be used like this

    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            // Do whatever you want here
        }
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // Do whatever you want here
        }
    };
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    

    copied from http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html

    UPDATE setDrawerListener method was deprecated in API level 24.1.0. so use addDrawerListener()

提交回复
热议问题