【android】onInterceptTouchEvent与onTouchEvent

以下都是翻译自官方网站,半搬运半翻译,可能翻译的不大好,有错误希望可以指出来,我会改正的 。

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:

  1. You will receive the down event here.
  2. 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.
  3. 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().
  4. 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将按以下顺序收到:

  1. 你将会在 ViewGroup.onInterceptTouchEvent()收到 down 事件。
  2. down 事件可以交由该 viewgroup 的一个子 view 处理,也可以由自己的onTouchEvent()处理;这意味着你需要实现onTouchEvent()并返回 true,这样你将会继续收到手势的后续部分(而不是去找父 view 去处理它)。同时,通过onTouchEvent()返回 true,你将不再从onInterceptTouchEvent()接收任何后续 events,并且所有触摸处理必须发生在onTouchEvent()
  3. 只要这个方法里面返回 false,那么这个 event 序列中的后续 events 将首先交付到这里,然后在交给目的 view 的onTouchEvent()
  4. 如果这个方法返回 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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值