android触摸事件处理流程,面试宝典

本文详细解析了Android中触摸事件的处理流程,包括Activity的dispatchTouchEvent、ViewGroup的onInterceptTouchEvent和onTouchEvent之间的交互。介绍了ACTION_DOWN、ACTION_MOVE等事件的传递规则,并通过不同返回值的情况分析了事件的拦截与消费。此外,还探讨了当重写onTouchEvent后,如何处理onClick和onLongClick事件的问题,揭示了Android内部如何区分这两种事件的机制。
摘要由CSDN通过智能技术生成

     public boolean dispatchTouchEvent (MotionEvent ev)

Activity中解释:

Called to process touch screen events.You can override this to intercept all touch screen events before they aredispatched to the window. Be sure to call this implementation for touch screenevents that should be handled normally.

Parameters

ev

The touch screen event.

Returns

·        boolean Return true if this event was consumed.

它会被调用处理触摸屏事件,可以重写覆盖此方法来拦截所有触摸屏事件在这些事件分发到窗口之前。通常应该处理触摸屏事件,一定要调用这个实现。当返回值为true时,表示这个事件已经被消费了。例如在TextActivity中dispatchTouchEvent在ACTION_MOVE返回true,运行结果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z5YKbaDp-1637305472132)(http://hunankeda110.iteye.com/admin/blogs/)]

也就是它并没有把那ACTION_MOVE分发下去。

public boolean onInterceptTouchEvent (MotionEvent ev)

Implementthis method to intercept all touch screen motion events. This allows you towatch events as they are dispatched to your children, and take ownership of thecurrent gesture at any point.

Usingthis function takes some care, as it has a fairly complicated interaction with [View.

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

onTouchEvent(MotionEvent)]( ),and using it requires implementing that method as well as this one in thecorrect 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 viewgroup, or given to your own onTouchEvent() method to hand

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值