在Android应用中使用DrawerLayout时手势无法工作

10

我有一个只有一个Activity的安卓应用。这个Activity使用以下布局:

<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);
}

我的问题是手势没有被检测到(虽然我可以通过滑动手势打开抽屉)。如果我使用线性布局代替drawerlayout,则不会出现这个问题,因此原因是导航抽屉。我做错了什么?


你解决了这个问题吗? - owe
我还没有时间测试@vivoila的解决方案,但是我下周会测试。我会告诉你结果。 - David Moreno García
我有一个类似的问题:https://dev59.com/dHvZa4cB1Zd3GeqP_iRX#SuQLoYgBc1ULPQZFgfMd。在我更新问题之前,我尝试了@vivoila的解决方案,但这对我没用。 - owe
你得到答案了吗? - Erika
1个回答

3

您需要为抽屉布局设置一个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;
        }
    });

返回gestureDetector.onTouchEvent(event); - Hugh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接