How to disable ViewPager from swiping in one direction

后端 未结 4 2080
清酒与你
清酒与你 2020-12-02 20:36

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

4条回答
  •  误落风尘
    2020-12-02 20:53

    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);
    

提交回复
热议问题