DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含义和区别

1.DispatchTouchEvent

     Pass the touch screen motion event down to the target view, or this view if it is the target.
     传递触摸事件到目标视图,或者他就是目标视图。


情况1:return true

则事件只会在当前方法内消耗掉,且不会向下传递,最后传给Activity.
情况2:return false  

则事件只会在当前方法内消耗不会向下传递,最后传给Activity。
但是不同于情况1,呈U型传递的过程不会传到当前的布局

情况3:super.dispatchTouchEvent(MotionEvent)

  整个过程会经历当前ViewGroup的三个触摸事件,dispatchTouch -> interceptTouch -> onTouchEvent。



2.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:
     使用这个方法需要注点意, 因为它和OnTouchEvent有着非常复杂的交互,在使用它的时候一样需要实现这个方法并且正确使用。事件将会按照以下顺序接收:
     
  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.
    按下事件将会被这视图里面的子视图处理,否则就被分发onTouchEvent 方法去处理 ; 这就意味着你应该实现 onTouchEvent 返回true, 所以你将会继续看到剩下的手势。而且,通过从onTouchEvent方法返回true,你不会在onInterceptTouchEvent方法中接收任何事件并且所有的触摸处理一定会在onTouchEvent中正常发生。

  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().
    只要你从这个函数返回false,每个紧跟着的事件将会 传递到这里然后分发到目标的的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.
    如果你返回true,你就不会接收到以下事件:目标视图将接收到同样的事件而且只有MotionEvent.Action_Cancel,而且所有未来的事件将会传递到你的onTouchEvent 方法,并且不在会出现在这里。

情况1:return true
事件传递将按照dispatch-> intercept-> onTouch进行传递,最终返回到Activity。
情况2:return false
正常传递
情况3: super.onInterceptTouchEvent(ev);

正常传递






3.OnTouchEvent

     Implement this method to handle touch screen motion events.
     无论触摸事件是否被处理,true || false 事件都会向下进行传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值