【问题标题】:Scrollable MapView inside of a RecyclerView: dispatchTouchEvent vs onTouchEventRecyclerView 内的可滚动 MapView:dispatchTouchEvent 与 onTouchEvent
【发布时间】: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


【解决方案1】:

处理事件的调用顺序大致如下: onInterceptTouchEvent、onDispatchTouchEvent、dispatchTouchEvent、onTouchEvent。 对我来说,这表明 onTouchEvent 是处理事件的最后一步。在最后一步操纵事件的处理地点和人员为时已晚。如果您查看早期处理事件的方法,源代码说明了什么?

【讨论】:

  • 确实,onTouchEvent 是最后调用的。但据我了解,调用 onTouchEvent() 的是 dispatchTouchEvent() 方法,所以在 dispatchMethod() 开始的某个地方,事件会丢失。但我不明白为什么会这样......
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 2016-12-30
  • 2015-09-11
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
相关资源
最近更新 更多