网上找到的禁止滑动答案是在dispathchTouchEvent()
方法里返回true,但是这样,其中的子控件也无法响应点击事件了。有人说重写子控件,使用getParent().requestDisallowInterceptTouchEvent()
设置ViewPager不拦截点击事件。但是我有很多子控件,该如何实现这样的需求呢?
迷茫2017-04-17 15:50:10
Rewrite ViewPager
@Override
public boolean onTouchEvent(MotionEvent ev) {
return isScrollable && super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return isScrollable && super.onInterceptTouchEvent(ev);
}
isScrollable is whether sliding is allowed, preventing Viewpager events, and child controls can also respond to events
ringa_lee2017-04-17 15:50:10
Rewrite touch events to cancel left and right sliding. Note that the overridden object is ViewPager, not Activity.
mRecoverPwdVp.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
大家讲道理2017-04-17 15:50:10
In the previous question of the questioner, I mentioned that this problem is actually about the understanding of touch event distribution. Google the distribution mechanism of touch events, and then you can look back at this issue after understanding it. Hope this helps!
高洛峰2017-04-17 15:50:10
Rewrite ViewPager as follows
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;//不要拦截事件 事件要分发给后面的控件
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;//不自己去处理事件
}