0

I have a class that extends ViewPager and contains the following methods:

@Override
public boolean onTouchEvent(MotionEvent event) {
    return isSwipeAllowed(event) && super.onTouchEvent(event);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    return isSwipeAllowed(event) && super.onInterceptTouchEvent(event);
}

/**
 * Only allow forward flow in tutorial screens. This method returns false if user tries to
 * drag from left to right, i.e. trying to get to a previous string
 */
private boolean isSwipeAllowed(MotionEvent event) {
    if(event.getAction()==MotionEvent.ACTION_DOWN) {
        initialXValue = event.getX();
        return true;
    }

    if(event.getAction()==MotionEvent.ACTION_MOVE) {
        try {
            float diffX = event.getX() - initialXValue;
            if (diffX > 0 ) {
                // swipe from left to right detected
                return false;
            }else if (diffX < 0 ) {
                // swipe from right to left detected
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    return true;
} 

Which works for preventing a user from dragging simply left-to-right to go back to the previous fragment. However if a user drags slightly right-to-left and then quickly left-to-right in the same swipe, the previous fragment is again brought into focus.

How can I prevent this?

-Otterman

Otterman
  • 530
  • 2
  • 5
  • 16

0 Answers0