Android如何禁用`ViewPager`滑动

创建一个基类继承ViewPager覆写方法。

public class CustomViewPager extends ViewPager {
    
    // `false` 为禁用滑动。
    private boolean isPagingEnabled = true;
 
    public CustomViewPager(Context context) {
        super(context);
    }
 
    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }
 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }
 
    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }}

你可能感兴趣的:(Android如何禁用`ViewPager`滑动)