码迷,mamicode.com
首页 > 其他好文 > 详细

DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含义和区别

时间:2015-03-05 19:29:35      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:触摸机制   ontouch   touch事件   

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 事件都会向下进行传递。

DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含义和区别

标签:触摸机制   ontouch   touch事件   

原文地址:http://blog.csdn.net/ykttt1/article/details/44085675

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!