关于onInterceptHoverEvent方法

今天在写一个处理用户touch事件的demo的时候顺手敲了前面几个字母,然后也没有仔细看智能提示,写完之后父控件始终拦截不到用户的任何Touch事件,最好猛地发现我写的是onInterceptHoverEvent这么个东西,看起来和onInterceptTouchEvent很像。查阅资料发现的确很像,只是所拦截的事件不一样而已,那么我onInterceptHoverEvent拦截的是什么事件呢,我们先来看看API文档。

public boolean onInterceptHoverEvent (MotionEvent event)

Added in API level 14
Implement this method to intercept hover events before they are handled by child views.

This method is called before dispatching a hover event to a child of the view group or to the view group's own onHoverEvent(MotionEvent) to allow the view group a chance to intercept the hover event. This method can also be used to watch all pointer motions that occur within the bounds of the view group even when the pointer is hovering over a child of the view group rather than over the view group itself.

The view group can prevent its children from receiving hover events by implementing this method and returning true to indicate that it would like to intercept hover events. The view group must continuously return true from onInterceptHoverEvent(MotionEvent) for as long as it wishes to continue intercepting hover events from its children.

Interception preserves the invariant that at most one view can be hovered at a time by transferring hover focus from the currently hovered child to the view group or vice-versa as needed.

If this method returns true and a child is already hovered, then the child view will first receive a hover exit event and then the view group itself will receive a hover enter event in onHoverEvent(MotionEvent). Likewise, if this method had previously returned true to intercept hover events and instead returns false while the pointer is hovering within the bounds of one of a child, then the view group will first receive a hover exit event in onHoverEvent(MotionEvent) and then the hovered child will receive a hover enter event.

The default implementation always returns false.

Parameters
event	The motion event that describes the hover.
Returns
True if the view group would like to intercept the hover event and prevent its children from receiving it.

这个方法是在API14,也就是Android4.0才添加的新方法,原理与onInterceptTouchEvent一样。在dispatch分发用户的HoverEvent事件的时候,返回true就代表消耗这个事件,不交给子View,若返回false代表自身不消耗这个事件,交给下一级子View来进行分发或处理。看到这里应该都明白这个方法的作用了,只是把TouchEvent换成了HoverEvent。那么什么是HoverEvent呢?像ACTION_DOWN,ACTION_MOVE,ACTION_UP这些都是TouchEvent一样,HoverEvent并不是一个类名,也只是一些MotionEvent。通过查阅API文档,发现这样几个event,ACTION_HOVER_ENTER, ACTION_HOVER_EXIT, ACTION_HOVER_MOVE,再来看一下文档中是怎么解释这些event的。

public static final int ACTION_HOVER_ENTER

Added in API level 14
Constant for getActionMasked(): The pointer is not down but has entered the boundaries of a window or view.

This action is always delivered to the window or view under the pointer.

This action is not a touch event so it is delivered to onGenericMotionEvent(MotionEvent) rather than onTouchEvent(MotionEvent).

Constant Value: 9 (0x00000009)
public static final int ACTION_HOVER_EXIT

Added in API level 14
Constant for getActionMasked(): The pointer is not down but has exited the boundaries of a window or view.

This action is always delivered to the window or view that was previously under the pointer.

This action is not a touch event so it is delivered to onGenericMotionEvent(MotionEvent) rather than onTouchEvent(MotionEvent).

Constant Value: 10 (0x0000000a)
public static final int ACTION_HOVER_MOVE

Added in API level 12
Constant for getActionMasked(): A change happened but the pointer is not down (unlike ACTION_MOVE). The motion contains the most recent point, as well as any intermediate points since the last hover move event.

This action is always delivered to the window or view under the pointer.

This action is not a touch event so it is delivered to onGenericMotionEvent(MotionEvent) rather than onTouchEvent(MotionEvent).

Constant Value: 7 (0x00000007)

Enter事件,is not down但是处于Window或者View的范围内,Exit事件,is not down但是已经来开Window或者View的范围,Move事件,我猜测应该是一些Enter事件,但是明确强调了这不是Touch事件,和ACTION_MOVE不一样,这个是is not down的。结合Hover的意思,这些应该是一些悬浮在界面上面的事件。做过网页的就应该很好理解了,这些就是鼠标的一些事件,悬停在View上,离开View,在View上面移动。那么安卓可以连接鼠标吗?当然是可以的,OTG功能应该出来很多年了,我记得我大一还是大二时候买的一个安卓平板就可以连接U盘。目前OTG虽然还不是手机标配功能,但是在平板上面应该见怪不怪了。为了方便用户利用OTG功能连接鼠标进行操作,安卓从4.0开始加入了这个方法,可以监听用户鼠标的一些操作。自己的手机不支持OTG,还没有亲自实验过,相信应该是没有问题的。

有任何疑问,欢迎加群讨论:261386924

文章同步更新于微信公众号: 秉心说 , 专注 Java 、 Android 原创知识分享,LeetCode 题解,欢迎关注!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值