disable the swipe gesture that opens the navigation drawer in android

霸气de小男生 提交于 2019-11-26 18:54:21

问题


I've been following the navigation drawer guide by Google and I'd like to add it to an Activity with tabs and gestures.

I'd like to disable the gesture to open the navigation drawer, does anyone have any idea how to do this?


回答1:


You should use:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

It worked for me, the swipe to open the drawer was disabled.

If it still won't work, check out the answer provided here.




回答2:


for locking you can do this:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

and for unlock :

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);



回答3:


Add gravity value too when using setDrawerLockMode();

Do this :

drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END);

This should work like a charm




回答4:


To disable swiping, override onInterceptTouchEvent and onTouchEvent on DrawerLayout and have them return false.




回答5:


The answer to disable swiping is the correct one. I think LOCK_MODE_LOCKED_CLOSED worked in Compat 24.x, but the functionality has been changed in newer compat libraries and LOCK_MODE_LOCKED_CLOSED now completely prevents the nav menu from showing, even via using the hamburger menu.

The following class works for me (Kotlin):

class MyDrawerLayout(ctx: Context) : DrawerLayout(ctx) {
  var isSwipeOpenEnabled: Boolean = true

  override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
      if (!isSwipeOpenEnabled && !isDrawerVisible(Gravity.START)) {
          return false
      }
      return super.onInterceptTouchEvent(ev)
  }

  @SuppressLint("ClickableViewAccessibility")
  override fun onTouchEvent(ev: MotionEvent): Boolean {
      if (!isSwipeOpenEnabled && !isDrawerVisible(Gravity.START)) {
          return false
      }
      return super.onTouchEvent(ev)
  }
}


来源:https://stackoverflow.com/questions/17051104/disable-the-swipe-gesture-that-opens-the-navigation-drawer-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!