要实现viewpager的滑动以及禁止滑动切换功能只需要继承viewpager,在onTouchEvent进行逻辑判断即可(网上搜到的,确实可行,原创地址不明),下面自己实现一个
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent; public class MyViewPager extends ViewPager { private boolean scrollble = true; public MyViewPager(Context context) {
super(context);
} public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean onTouchEvent(MotionEvent ev) {
if (!scrollble) {
return true;
}
return super.onTouchEvent(ev);
} public boolean isScrollble() {
return scrollble;
} public void setScrollble(boolean scrollble) {
this.scrollble = scrollble;
}
}
使用时只需要根据需要调用setScrollble(true/false)即可进行切换!!!