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 (in Java)
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;
}
}
2.Extend ViewPager (in Kotlin)
enum class SwipeDirection {
ALL, LEFT, RIGHT, NONE
}
class SingleDirectionViewPager @JvmOverloads constructor(
context: Context,
attrs: AttributeSet?
) : ViewPager(context, attrs) {
private var initialXValue = 0f
private var direction: SwipeDirection = SwipeDirection.ALL
override fun onTouchEvent(event: MotionEvent): Boolean =
if (isSwipeAllowed(event)) {
super.onTouchEvent(event)
} else {
false
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean =
if (isSwipeAllowed(event)) {
super.onInterceptTouchEvent(event)
} else {
false
}
private fun isSwipeAllowed(event: MotionEvent): Boolean {
if (direction == SwipeDirection.ALL) {
return true
}
if (direction == SwipeDirection.NONE) {
return false
}
if (event.action == MotionEvent.ACTION_DOWN) {
initialXValue = event.x
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
}
fun setAllowedSwipeDirection(direction: SwipeDirection) {
this.direction = direction
}
}
3.Use your viewPager in a layout
<package_name.customviewpager
android:id="@+id/customViewPager"
android:layout_height="match_parent"
android:layout_width="match_parent" />
4.Enable any swipe direction in code. Default is all (right and left)
mViewPager.setAllowedSwipeDirection(SwipeDirection.RIGHT);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…