Search code examples
androidscrollviewgesture

Horizontal swipe not detected in ScrollView's parent


Possible Duplicate:
Gesture detection and ScrollView issue

EDIT: question with full code asked here.


I've got a layout with a child. I set a gesture listener to detect horizontal swipe on the layout. When the layout is a LinearLayout the swipe is properly detected, but when it's a ScrollView, it's not. I guess the gesture is first detected by the ScrollView and is not propagated to its ascendants, but I don't know how to solve it.

Here's my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <ImageView android:layout_width="320dp" android:layout_height="30dp"
            android:src="@drawable/header"/>
    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
        <!-- stuff -->
    </ScrollView>
</LinearLayout>

I set the following listener to my layout:

class ProductGestureListener extends SimpleOnGestureListener {

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        final int SWIPE_MIN_DISTANCE = 120;
        final int SWIPE_MAX_OFF_PATH = 250;
        final int SWIPE_THRESHOLD_VELOCITY = 200;            

        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {                   
                // show previous item
            }  else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               // show next item
            }
        } catch (Exception e) {
        }           
        return false;
    }
}

Solution

  • I had to add

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        super.dispatchTouchEvent(ev);    
        return productGestureDetector.onTouchEvent(ev); 
    }
    

    Add this method in your activity class which uses swiper just like the onCreate method.