Scrollable MapView inside of a RecyclerView: dispatchTouchEvent vs onTouchEvent

倖福魔咒の 提交于 2020-01-03 03:36:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!