国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含义和区别

DispatchTouchEvent & OnInterceptTouchEvent & onTouchEvent的含义和区别

来源:程序员人生   发布时间:2015-03-25 11:52:06 阅读次数:3616次
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的3个触摸事件,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样需要实现这个方法并且正确使用。事件将会依照以下顺序接收:
     
  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方法中接收任何事件并且所有的触摸处理1定会在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 事件都会向下进行传递。
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生