【问题标题】:Gestures not working when using DrawerLayout in Android app在 Android 应用中使用 DrawerLayout 时手势不起作用
【发布时间】:2013-07-26 18:38:28
【问题描述】:

我有一个带有单个 Activity 的 Android 应用。此活动使用此布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

然后,在我的源代码中,我在构造函数中定义了两个手势检测器:

mDetector = new GestureDetectorCompat(this, new CustomGestureListener());
mScaleDetector = new ScaleGestureDetector(this, new CustomScaleGestureListener());

我正在以这种方式覆盖 onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getPointerCount() > 1) {
        this.mScaleDetector.onTouchEvent(event);
    } else {
        this.mDetector.onTouchEvent(event);
    }
    return super.onTouchEvent(event);
}

我的问题是没有检测到手势(尽管我可以用滑动手势打开抽屉)。如果我将抽屉布局替换为例如线性布局,则不会出现此问题,因此原因是导航抽屉。我做错了什么?

【问题讨论】:

  • 你有解决这个问题的办法吗?
  • 我没有时间测试@vivoila 解决方案,但我会在下周进行。我会告诉你一些事情。
  • 我遇到了类似的问题:stackoverflow.com/questions/21040150/…。在我对我的问题进行更新之前,我测试了@vivoila 解决方案,但这对我不起作用。
  • 你得到答案了吗?

标签: java android gesture-recognition drawerlayout


【解决方案1】:

您必须为您的抽屉布局设置一个 TouchListener。 例如/

gestureDetector = new GestureDetector(this, new CustomGestureListener());

    mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2015-10-21
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    相关资源
    最近更新 更多