Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gestures not working when using DrawerLayout in Android app

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?

like image 405
David Moreno García Avatar asked Jul 26 '13 13:07

David Moreno García


People also ask

How do you use navigation gestures?

2-button navigation — The gesture navigation introduced in Android 9.0 Pie, with a thin line as the Home button and a Back button appearing as needed. Gesture Navigation — Tap this option to turn it on. To turn it back off, just tap one of the other options in the same menu.

How do you use DrawerLayout?

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.


1 Answers

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;
        }
    });
like image 155
vivoila Avatar answered Nov 15 '22 20:11

vivoila