public class CustomViewPager extends ViewPager { 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; } }
自定义一个CustomViewPager 继承 ViewPager;重写onTouch 和 onInterceptTouchEvent方法,在获得viewpager对象时,通过viewpager.setPagingEnabled(false)就可以禁止ViewPager滑动。
时间: 2024-10-03 14:28:33