Horizontal swipe not detected in ScrollView's parent

jul picture jul · Nov 30, 2011 · Viewed 10.1k times · Source

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

Answer

kaspermoerch picture kaspermoerch · Nov 30, 2011

If you want the whole Activity to be swipeable horizontally you can use the following as a super class for your Activity:

public abstract class SwipeActivity extends Activity {
   private static final int SWIPE_MIN_DISTANCE = 120;
   private static final int SWIPE_MAX_OFF_PATH = 250;
   private static final int SWIPE_THRESHOLD_VELOCITY = 200;
   private GestureDetector gestureDetector;

   @Override
   protected void onCreate( Bundle savedInstanceState ) {
      super.onCreate( savedInstanceState );
      gestureDetector = new GestureDetector( new SwipeDetector() );
   }

   private class SwipeDetector extends SimpleOnGestureListener {
      @Override
      public boolean onFling( MotionEvent e1, MotionEvent e2, float velocityX, float velocityY ) {

         // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH,
         // then dismiss the swipe.
         if( Math.abs( e1.getY() - e2.getY() ) > SWIPE_MAX_OFF_PATH )
            return false;

         // Swipe from right to left.
         // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
         // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
         if( e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs( velocityX ) > SWIPE_THRESHOLD_VELOCITY ) {
            next();
            return true;
         }

         // Swipe from left to right.
         // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
         // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
         if( e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs( velocityX ) > SWIPE_THRESHOLD_VELOCITY ) {
            previous();
            return true;
         }

         return false;
      }
   }

   @Override
   public boolean dispatchTouchEvent( MotionEvent ev ) {
      // TouchEvent dispatcher.
      if( gestureDetector != null ) {
         if( gestureDetector.onTouchEvent( ev ) )
            // If the gestureDetector handles the event, a swipe has been
            // executed and no more needs to be done.
            return true;
      }
      return super.dispatchTouchEvent( ev );
   }

   @Override
   public boolean onTouchEvent( MotionEvent event ) {
      return gestureDetector.onTouchEvent( event );
   }

   protected abstract void previous();

   protected abstract void next();
}

All you need to do is implement the next() and previous() methods after extending SwipeActivity.