【发布时间】:2017-04-25 11:05:30
【问题描述】:
我试图在 RecyclerView 中添加一个滚动的 MapView,因此我在 TouchEvent 之前和之后设置了 requestDisallowInterceptTouchEvent()。
奇怪的是:如果我在 dispatchTouchEvent() 方法中设置它确实有效,但如果我在 onTouchEvent() 方法中设置它则不起作用。
有人可以解释为什么我不能在onTouchEvent() 中设置它吗?
工作:
public class WorkingScrollableListItemMapView extends MapView {
// constructors
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// Stop parents from handling the touch
this.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow parents from handling the touch
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return super.dispatchTouchEvent(ev);
}
}
不工作:
public class NotWorkingScrollableListItemMapView extends MapView {
// constructors
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
// Allow parents from handling the touch
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_DOWN:
// Stop parents from handling the touch
this.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
return super.onTouchEvent(ev);
}
}
【问题讨论】:
-
你能解释一下“不工作”是什么意思吗?是不是说,父
ViewGroup还在拦截这个事件? -
是的,所以水平滚动仍然可以工作(但速度很慢),但垂直滚动根本不行。
标签: android scroll android-recyclerview