我一直遵循谷歌的导航抽屉指南,我想将它添加到一个带有选项卡和手势的活动。

我想禁用打开导航抽屉的手势,有人知道怎么做吗?


当前回答

这对我很有用

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, {Your drawer view});

其他回答

禁用滑动的答案是正确的。我认为LOCK_MODE_LOCKED_CLOSED在Compat 24中工作。x,但功能已在更新的compat库和LOCK_MODE_LOCKED_CLOSED现在完全阻止导航菜单显示,甚至通过使用汉堡菜单。

下面的类适用于我(Kotlin):

class MyDrawerLayout @JvmOverloads constructor(
    ctx: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : DrawerLayout(ctx, attrs, defStyleAttr) {
      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)
      }
    }

使用setDrawerLockMode()时也添加重力值;

这样做:

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

这应该很有效

这对我很有用

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, {Your drawer view});

你应该使用:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

它对我有用,滑动打开抽屉被禁用了。

如果仍然不能工作,请查看这里提供的答案。

要禁用滑动,重写DrawerLayout上的onInterceptTouchEvent和onTouchEvent,并让它们返回false。