1.写一个类继承ViewPager,重写里面的构造方法、onInterceptTouchEvent、onTouchEvent方法
public class CustomViewPager extends ViewPager { private boolean result = false; public CustomViewPager(Context context) { super(context); } public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (result) { return super.onInterceptTouchEvent(ev); } else { return false; } } @Override public boolean onTouchEvent(MotionEvent ev) { if (result) { return super.onTouchEvent(ev); } else { return false; } } }
2.把xml里的布局ViewPager给换成你刚刚自定义的这个类
<com.jiyun.dell.xiongmaolive.activity.CustomViewPager android:id="@+id/viewpagerhome" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/view" />

本文介绍了一种通过自定义ViewPager类来控制滑动行为的方法。具体实现包括重写构造方法及触摸事件处理方法,使ViewPager能够按需响应用户的触摸操作。此外,还展示了如何将自定义的ViewPager应用到Android项目的XML布局文件中。





