Search code examples
androidandroid-viewpagertouch-event

ViewPager swipe with 2 fingers


I would to implement a ViewPager swipe with two fingers. I tried to implement a sublcass of ViewPager overriding the onTouchEvent and passing the method to superclass only if the touch is made by 2 fingers. But there is a problem: the swipe animation also works with 1 finger! I think I have to override some other method...

This is my ViewPager class:

public class MyViewPager extends ViewPager{
public MyViewPager(Context context) {
    super(context);
}

public MyViewPager(Context context,AttributeSet attributeSet) {
    super(context,attributeSet);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int n = ev.getPointerCount(); //number of fingers
    if (n == 2)
        return super.onTouchEvent(ev);
    else return false;
}
}

Solution

  • Overriding onInterceptTouchEvent() should do the trick. According to the comments in the ViewPager sources, it's there where the decision is made whether scrolling should start or not.