就是重写ViewPager的
onInterceptTouchEvent(MotionEvent arg0)方法和onTouchEvent(MotionEvent
arg0)方法,这两个方法的返回值都是boolean类型的,只需要将返回值改为false,那么ViewPager就不会消耗掉手指滑动的事件了,转而传递给上层View去处理或者该事件就直接终止了。
public class MyViewPager extends ViewPager {
// private static final String TAG = "ViewPager";
//是否禁止左右滑动
private boolean disableScroll=false;
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// LogHelper.i(TAG, "MyViewPager dispatchTouchEvent, ");
if (getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if(disableScroll){
return false;
}
return super.onInterceptTouchEvent(arg0);
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
if (disableScroll)
return false;
else
return super.onTouchEvent(arg0);
}
public boolean isDisableScroll() {
return disableScroll;
}
public void setDisableScroll(boolean disableScroll) {
this.disableScroll = disableScroll;
}
}
阅读(249) | 评论(0) | 转发(0) |