Gestures not working when using DrawerLayout in Android app

杀马特。学长 韩版系。学妹 提交于 2019-12-21 04:00:08

问题


I have an Android app with a single Activity. This activity uses this layout:

<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>

Then, in my source I have two gesture detectors defined in the constructor:

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

And I'm overriding onTouchEvent this way:

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

My problem is that gestures are not detected (although I can open the drawer with a swipe gesture). This problem doesn't occur if I replace the drawerlayout with, for example, a linear layout, so the cause is the navigation drawer. What am I doing wrong?


回答1:


you have to set a TouchListener for your drawer layout. eg/

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


来源:https://stackoverflow.com/questions/17882255/gestures-not-working-when-using-drawerlayout-in-android-app

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