Handling scroll using GestureDetector in a ViewPager
<p>I have a ViewPager with 2 fragments; in the first fragment I need to perform an action if the user scrolls up or down. The ViewPager eats the touch events, so I extended ViewPager (I found something similar in another question ). The problem is that doing so, even if I scroll vertically, the viewpager starts moving the page. So I changed the onTouchEvent method to check for a minimum horizontal scroll. It works but I think the solution is not particulary good and sometimes the page change lags a bit. Another problem with this solution is that the second fragment will have a list view, and like this I think I'll pass the vertical scroll to the Gesturedetector.
Here my code</p>

<pre><code>public class GestureDetectorViewPager extends ViewPager {

private static final String TAG = GestureDetectorViewPager.class.getName();
private static final int MIN_HORIZ_SCROLL = 20;

private GestureDetector gestureDetector;
private int mLastX;

public GestureDetectorViewPager(Context context) {
    super(context);
}

public GestureDetectorViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setGestureDetector(GestureDetector gestureDetector) {
    this.gestureDetector = gestureDetector;
}



@Override
public boolean onTouchEvent(MotionEvent e) {

    int deltaX = (int) ((mLastX &gt; e.getRawX()) ? (mLastX - e.getRawX())
            : (e.getRawX() - mLastX));

    mLastX = (int) e.getRawX();
    if (deltaX &lt;= MIN_HORIZ_SCROLL &amp;&amp; e.getAction() == MotionEvent.ACTION_MOVE) {

        return gestureDetector.onTouchEvent(e);

    } else {

        return super.onTouchEvent(e);

    }
}
</code></pre>

<p>}</p>

<p>Is there a better solution to this problem?</p>

<p><strong>UPDATE</strong></p>

<p>The ListFragment in the other page works fine</p>
