安卓:onInterceptTouchEvent和dispatchTouchEvent之间的区别?区别、onInterceptTouchEvent、dispatchTouchEvent

2023-09-12 00:39:42 作者:别让梦想只是梦想

在Android的 onInterceptTouchEvent dispatchTouchEvent 之间的区别是什么?

What is the difference between onInterceptTouchEvent and dispatchTouchEvent in Android?

据Android开发指南,这两种方法可以用来拦截触摸事件( MotionEvent ),但有什么区别呢?

According to the android developer guide, both methods can be used to intercept a touch event (MotionEvent), but what is the difference?

如何 onInterceptTouchEvent dispatchTouchEvent 的onTouchEvent 互动在视图(的ViewGroup )?

How do onInterceptTouchEvent, dispatchTouchEvent and onTouchEvent interact together within a hierarchy of Views (ViewGroup)?

推荐答案

神秘面纱,这是源$ C ​​$ C的最佳场所。该文档是远远不够的有关说明这一点。

The best place to demystify this is the source code. The docs are woefully inadequate about explaining this.

dispatchTouchEvent实际上是在活动,查看和ViewGroup中定义。 把它作为一个控制器,它决定了触摸如何路由事件。

dispatchTouchEvent is actually defined on Activity, View and ViewGroup. Think of it as a controller which decides how to route the touch events.

例如,最简单的情况是, View.dispatchTouchEvent 这将路由触摸事件为 OnTouchListener.onTouch 如果它的定义或扩展方法的onTouchEvent

For example, the simplest case is that of View.dispatchTouchEvent which will route the touch event to either OnTouchListener.onTouch if it's defined or to the extension method onTouchEvent.

对于 ViewGroup.dispatchTouchEvent 事情的方式更加复杂。它需要找出其中的子视图应该得到事件(通过调用child.dispatchTouchEvent)。这基本上是一个命中测试算法,在那里你找出哪些子视图的边框包含触摸点坐标。

For ViewGroup.dispatchTouchEvent things are way more complicated. It needs to figure out which one of its child views should get the event (by calling child.dispatchTouchEvent). This is basically a hit testing algorithm where you figure out which child view's bounding rectangle contains the touch point coordinates.

但在此之前可以调度的事件到相应的子视图,家长可以窥探和/或截获该事件一起。这是 onInterceptTouchEvent 是有。因此,首先要调用此方法做的点击测试之前,如果该事件被劫持(通过返回来自onInterceptTouchEvent真)发送一个 ACTION_CANCEL 以孩子的意见,使他们可以放弃自己的触摸事件处理(由previous触摸事件),并从那时起在母公司层面的所有触摸事件将被指派为 onTouchListener.onTouch (如果定义)或的onTouchEvent ()。另外,在这种情况下,onInterceptTouchEvent从未再次调用

But before it can dispatch the event to the appropriate child view, the parent can spy and/or intercept the event all together. This is what onInterceptTouchEvent is there for. So it calls this method first before doing the hit testing and if the event was hijacked (by returning true from onInterceptTouchEvent) it sends a ACTION_CANCEL to the child views so they can abandon their touch event processing (from previous touch events) and from then onwards all touch events at the parent level are dispatched to onTouchListener.onTouch (if defined) or onTouchEvent(). Also in that case, onInterceptTouchEvent is never called again.

请问你甚至要覆盖【活动|的ViewGroup |查看] .dispatchTouchEvent?除非你是做一些自定义的路由可能是你不应该。

Would you even want to override [Activity|ViewGroup|View].dispatchTouchEvent? Unless you are doing some custom routing you probably should not.

主要的扩展方法是ViewGroup.onInterceptTouchEvent如果你想窥探和/或拦截触摸事件在父级别和View.onTouchListener / View.onTouchEvent主事件处理。

The main extension methods are ViewGroup.onInterceptTouchEvent if you want to spy and/or intercept touch event at the parent level and View.onTouchListener/View.onTouchEvent for main event handling.

所有的一切过于复杂的设计IMO但是Android的API更倾向于灵活性比简单。

All in all its overly complicated design imo but android apis lean more towards flexibility than simplicity.

 
精彩推荐
图片推荐