5
votes

I want to disable the opening of a drawer via swipe, but not the closing via the swipe or back button.

I'm using fragments in my drawer, so that when the drawer opens I replace my fragment and add it to the backstack. On pressing the backbutton, the drawer closes as aspected.

But when I use

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

like in this post: disable the swipe gesture that opens the navigation drawer in android it disables ALL swipes und the backbutton navigation. The only way to close the drawer now is to touch the screen outside of the drawer.

Is there an alterantive to the LockMode and preserve the swipe close and backbutton navigation?

Note: I'm using Android 5.0.1

1

1 Answers

10
votes

I found a workaround to achieve what I want:

Initialy I set the drawerMode to CLOSED. After opening it e.g. via a button, I UNLOCK the drawer to enable the gesture for closing again. After the drawer has been closed the Lock for opening will be reactivated. For this I'm using the Interface of the ActionBarDrawerToggle

    mDrawerToggle = new ActionBarDrawerToggle(
            getActivity(),                    /* host Activity */
            mDrawerLayout,                    /* DrawerLayout object */
            R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
            R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
    )
    {
        @Override
        public void onDrawerClosed(View drawerView)
        {
            super.onDrawerClosed(drawerView);
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }

        @Override
        public void onDrawerOpened(View drawerView)
        {
            super.onDrawerOpened(drawerView);
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);           
        }
    };

I tried to overide the drawerlayout or parts of it, but it would be a hell of work to get what I want.

I hope this solution is helpfull to others.