RecyclerView内部的可滚动MapView:dispatchTouchEvent与onTouchEvent [英] Scrollable MapView inside of a RecyclerView: dispatchTouchEvent vs onTouchEvent
问题描述
我正在尝试在RecyclerView中使用滚动的MapView,因此我在TouchEvent之前和之后设置了requestDisallowInterceptTouchEvent()
.
I'm trying to have a scrolling MapView inside of a RecyclerView, therefore I'm setting requestDisallowInterceptTouchEvent()
before and after the TouchEvent.
奇怪的是:如果我在dispatchTouchEvent()
方法中进行了设置,它确实可以工作,但是如果我在onTouchEvent()
方法中进行了同样的操作,则它就无法工作.
The odd thing is: this does work if I set it in the dispatchTouchEvent()
method, but it doesn't work if I do the same in the onTouchEvent()
method.
有人可以解释为什么我不能在onTouchEvent()
中进行设置吗?
Can somebody explain why I cannot set this in 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);
}
}
推荐答案
用于处理事件的调用顺序如下: onInterceptTouchEvent,onDispatchTouchEvent,dispatchTouchEvent,onTouchEvent. 对我而言,这表明onTouchEvent是处理事件的最后一步.要在何处&谁在最后一步处理事件.如果您查看处理事件的早期方法,源代码怎么说?
Call sequence for handling an event are somewhat in this order: onInterceptTouchEvent, onDispatchTouchEvent, dispatchTouchEvent, onTouchEvent. That, to me, indicates that the onTouchEvent is the very last step in processing an event. It would be too late to manipulate where & whom handles the event at the very last step. What does the source code say if you look at the earlier methods for handling the event?
这篇关于RecyclerView内部的可滚动MapView:dispatchTouchEvent与onTouchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!