Android开发ViewPager禁止左右滑动

仅做个人记录,不喜勿喷;

直接上代码

public class XViewPager extends ViewPager {

    // 是否禁止 viewpager 左右滑动
    private boolean xScroll = true;

    public void setSlide(boolean slide) {
        this.xScroll = slide;
    }

    public XViewPager(@NonNull Context context) {
        super(context);
    }

    public XViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (xScroll) {
            return false;
        } else {
            return super.onTouchEvent(arg0);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (xScroll) {
            return false;
        } else {
            return super.onInterceptTouchEvent(arg0);
        }
    }
}

你可能感兴趣的:(Activity,ViewPager)