ViewGroup.onInterceptTouchEvent和View.onTouchEvent分析

  这几天闲着没什么事,准备回顾下View的事件派发机制,看到按键事件派发时发现有些东西已经比较模糊了,所以整理了下网上相关的资料结合自己的理解写点东西,可以留着以后方便查看,也方便其他人找相关方面的资料。

  关于onInterceptTouchEvent(MotionEvent ev) :

  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 MotionEvent.ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

  根据官方API给的注释,有很多地方并不是很理解,所以还是根据我的理解重新总结一下(有错误希望指出):

  该方法是用来截获所有屏幕的触摸相关事件,然后可以根据需要做相应的处理。主要在使用时,所有的事件最终还是会在onTouchEvent方法中去处理(即事件传递终点);

  每次的触摸事件会经过以下传递路径(默认所有方法都返回false的情况下,下面的Down-->Move-->Up是指单独一次触摸触发的一组事件,所有触摸区域均发生在View区域);

  DOWN ::

  Activity.dispatchTouchEvent ---> ViewGroup1.onInterceptTouchEvent --->ViewGroup2.onInterceptTouchEvent ----> VIEW.onTouchEvent ----> ViewGroup2.onTouchEvent --->ViewGroup1.onTouchEvent ---> Activity.onTouchEvent;

  Move ::

  Activity.dispatchTouchEvent----> Activity.onTouchEvent;

  UP ::

  Activity.dispatchTouchEvent----> Activity.onTouchEvent;

  根据官方的解释:

  1、You will receive the down event here.----->所有的触摸事件的Down事件都会由root viewGroup.onInterceptTouchEvent方法最先接收(暂且不考虑dispatchTouchEvent返回true的情况,这种情况,事件就不可能会继续传递到下级View组件中来了,每个事件的传递都是先经过dispatchTouchEvent来派发然后再传入viewgroup.onInterceptTouchEvent),根据该方法的返回值会有不同的传递方法:

    a,false---> 会继续递归传递到下级viewGroup.onInterceptTouchEvent方法直到view.onTouchEvent,如果在该过程中没有任何方法消化该事件(返回true)则继续往回走调用自身的OnTouchEvent方法。路径同上面的传递路径;

    b,true---> 意味着该方法拦击了该触摸的down事件,并通过本身的onTouchEvent方法将该Down事件直接传递到父控件的onTouchEvent处理,子控件将不能接收到任何的事件包括这次down事件本身;例如:在ViewGroup2.onInterceptTouchEvent中返回true则,事件传递路径为:

  DOWN ::

  Activity.dispatchTouchEvent ---> ViewGroup1.onInterceptTouchEvent --->ViewGroup2.onInterceptTouchEvent(return true) ----> VIEW.onTouchEvent ----> ViewGroup2.onTouchEvent --->ViewGroup1.onTouchEvent ---> Activity.onTouchEvent;

  Move ::

  Activity.dispatchTouchEvent----> Activity.onTouchEvent;

  UP ::

  Activity.dispatchTouchEvent----> Activity.onTouchEvent;

 2、如果有每个控件的onTouchEvent方法消化了down事件,则会停止事件的继续传递,接下来的move和down事件也将会直接从该控件的父控件直接传递给自身的onTouchEvent方法不需要经过自身的onInterceptTouchEvent方法;例如:在ViewGroup2.onTouchEvent中返回true,事件传递路如下:

  DOWN ::

  Activity.dispatchTouchEvent ---> ViewGroup1.onInterceptTouchEvent --->ViewGroup2.onInterceptTouchEvent ----> VIEW.onTouchEvent ----> ViewGroup2.onTouchEvent(return true) --->ViewGroup1.onTouchEvent ---> Activity.onTouchEvent;

  Move ::

   Activity.dispatchTouchEvent ---> ViewGroup1.onInterceptTouchEvent ---> ViewGroup2.onTouchEvent;

  UP ::

  Activity.dispatchTouchEvent ---> ViewGroup1.onInterceptTouchEvent ---> ViewGroup2.onTouchEvent;

    

总结如下: 事件的派发和拦截是(dispatchTouchEvent、onInterceptTouchEvent) 从 父 到 子(parent ---> child);事件的处理机制(onTouchEvent) 是 从 子 到 父(child ---> parent);

如有不对的地方欢迎指出来,谢谢!

 

转载于:https://www.cnblogs.com/daxingxiang/p/4724078.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值