Gestures not working when using DrawerLayout in Android app
Asked Answered
M

1

10

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?

Mountaineer answered 26/7, 2013 at 13:12 Comment(4)
Did you get a solution for this problem?Fetid
I hadn't the time to test @vivoila solution but I'll do it next week. I'll tell you something.Ardel
I got a similar problem: #21040650. Before I made the updates in my question I tested @vivoila solution but this didn't work for me.Fetid
Did you get the answer?Gabionade
M
3

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;
        }
    });
Maupin answered 16/10, 2013 at 1:17 Comment(1)
or simply return gestureDetector.onTouchEvent(event);Metacarpal

© 2022 - 2024 — McMap. All rights reserved.