I want to allow the user swipe in a ViewPager only from right to left. So once he passed a page he can\'t come back to it. How can this be done?
I tried
There is one more event you miss: onInterceptTouchEvent. It`s must contain the same logic as onTouchEvent.
My complete solution is based on this answer. It will allow you to enable/disable paging in any direction in any time you need.
1. Create enum
public enum SwipeDirection {
ALL, LEFT, RIGHT, NONE ;
}
2.Extend ViewPager
public class CustomViewPager extends ViewPager {
private float initialXValue;
private SwipeDirection direction;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.direction = SwipeDirection.ALL;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.IsSwipeAllowed(event)) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.IsSwipeAllowed(event)) {
return super.onInterceptTouchEvent(event);
}
return false;
}
private boolean IsSwipeAllowed(MotionEvent event) {
if(this.direction == SwipeDirection.ALL) return true;
if(direction == SwipeDirection.NONE )//disable any swipe
return false;
if(event.getAction()==MotionEvent.ACTION_DOWN) {
initialXValue = event.getX();
return true;
}
if (event.action === MotionEvent.ACTION_MOVE) {
val diffX = event.x - initialXValue
if (diffX > 0 && direction === SwipeDirection.RIGHT) {
// swipe from left to right detected
return false
} else if (diffX < 0 && direction === SwipeDirection.LEFT) {
// swipe from right to left detected
return false
}
}
return true;
}
public void setAllowedSwipeDirection(SwipeDirection direction) {
this.direction = direction;
}
}
3.Use your viewPager in a layout
4.Enable any swipe direction in code. Default is all (right and left)
mViewPager.setAllowedSwipeDirection(SwipeDirection.RIGHT);