以下都是翻译自官方网站,半搬运半翻译,可能翻译的不大好,有错误希望可以指出来,我会改正的 。
onInterceptTouchEvent
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent)
, and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:
- You will receive the down event here.
- The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
- For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target’s onTouchEvent().
- If you return true from here, you will not receive any following events: the target view will receive the same event but with the action
ACTION_CANCEL
, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
使用此方法来截取所有触摸事件。当event被分发给子view时,它允许你监控 event,并随时掌握当前的手势。
使用此功能需要非常谨慎,因为它与View.onTouchEvent()
有着相当复杂的交互 ,使用它要求以正确的方式实现View.onTouchEvent()
和 ViewGroup.onInterceptTouchEvent()
。event将按以下顺序收到:
- 你将会在
ViewGroup.onInterceptTouchEvent()
收到 down 事件。 - down 事件可以交由该 viewgroup 的一个子 view 处理,也可以由自己的
onTouchEvent()
处理;这意味着你需要实现onTouchEvent()
并返回 true,这样你将会继续收到手势的后续部分(而不是去找父 view 去处理它)。同时,通过onTouchEvent()
返回 true,你将不再从onInterceptTouchEvent()
接收任何后续 events,并且所有触摸处理必须发生在onTouchEvent()
。 - 只要这个方法里面返回 false,那么这个 event 序列中的后续 events 将首先交付到这里,然后在交给目的 view 的
onTouchEvent()
。 - 如果这个方法返回 true,那么你将不会收到 event 事件序列中后续的 events,目标 view 将接收一个 event 但是伴随着一个 ACTION_CANCEL 事件,所有其他 events 将被分发到自己的
onTouchEvent()
方法,不会再出现在本方法。
本方法有一个返回值:如果返回true,将从子 view 截取 events,并且分发给自己的onTouchEvent()
处理。当前目标 view 将接收到一个 ACTION_CANCEL 事件,并不再接收其他消息。
onTouchEvent
Implement this method to handle touch screen motion events.
If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick()
. This will ensure consistent system behavior, including:
- obeying click sound preferences
- dispatching OnClickListener calls
- handling
ACTION_CLICK
when accessibility features are enabled
实现此方法来处理 motion events。
本方法有一个返回值:如果处理了 event ,就返回 true,否则返回 false。