问题
I'm trying to have a scrolling MapView inside of a RecyclerView, therefore I'm setting requestDisallowInterceptTouchEvent()
before and after the TouchEvent.
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.
Can somebody explain why I cannot set this in onTouchEvent()
?
Working:
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);
}
}
Not working:
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);
}
}
回答1:
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?
来源:https://stackoverflow.com/questions/43608941/scrollable-mapview-inside-of-a-recyclerview-dispatchtouchevent-vs-ontouchevent