`

android事件2-onInterceptTouchEvent和onTouchEvent调用时序

阅读更多

 本文源地址:http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html#

 

onInterceptTouchEvent()用于处理事件并改变事件的传递方向。返回值为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)

onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。

<?xml version="1.0" encoding="utf-8"?>
<com.touchstudy.LayoutView1 xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.touchstudy.LayoutView2
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center">
       <com.touchstudy.MyTextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv"
            android:text="AB"
            android:textSize="40sp"
            android:textStyle="bold"
            android:background="#FFFFFF"
            android:textColor="#0000FF"/>
   </com.touchstudy.LayoutView2>
</com.touchstudy.LayoutView1>

 

 

在没有重写onInterceptTouchEvent()onTouchEvent()的情况下(他们的返回值都是false), 对上面这个布局,MotionEvent事件的传递顺序如下:

 

 

当某个控件的onInterceptTouchEvent()返回值为true时,就会发生截断,事件被传到当前控件的onTouchEvent()。如我们将LayoutView2onInterceptTouchEvent()返回值为true,则传递流程变成:

 

 

 如果我们同时将LayoutView2onInterceptTouchEvent()onTouchEvent()设置成true,那么LayoutView2将消费被传递的事件,同时后续事件(如跟着ACTION_DOWNACTION_MOVE或者ACTION_UP)会直接传给LayoutView2onTouchEvent(),不传给其他任何控件的任何函数。同时传递给子控件一个ACTION_CANCEL事件。传递流程变成(图中没有画出ACTION_CANCEL事件):

 

      
LayoutView2.onTouchEvent().ACTION_DOWN
会调用LayoutView1.OntnterceptTouchEvent().ACTION_MOVE
如下:

05-25 07:02:44.149: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_DOWN
05-25 07:02:44.149: DEBUG/LayoutView2(1820): onInterceptTouchEvent action:ACTION_DOWN
05-25 07:02:44.149: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_DOWN
05-25 07:02:44.179: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.179: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.210: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.219: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.239: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.249: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.269: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.279: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.299: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.299: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.329: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.329: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.359: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.359: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.379: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_MOVE
05-25 07:02:44.389: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_MOVE
05-25 07:02:44.389: DEBUG/LayoutView1(1820): onInterceptTouchEvent action:ACTION_UP
05-25 07:02:44.399: DEBUG/LayoutView2(1820): onTouchEvent action:ACTION_UP

 

 

SDK给出的说明:

·  You will receive the down event here.

·  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.

·  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().

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

 

来自http://hi.baidu.com/j_fo/blog/item/7321c91324203437dc54017d.html

总结:

onInterceptTouchEvent()说的是是否允许Touch事件继续向下(子控件)传递,一但返回True,则向下传递之路被截断(所有子控件将没有机会参与Touch事件);

onTouchEvent()说的是当前控件在处理完Touch事件后,是否还允许Touch事件继续向上(父控件)传递,一但返回True,则父控件不用操心,由自己来处理Touch事件。

 

实例见附件:

  • 大小: 12.8 KB
  • 大小: 12.4 KB
  • 大小: 13.3 KB
  • 大小: 14.7 KB
分享到:
评论
2 楼 WH_Always 2011-11-16  
demo不能运行啊。。。。。
1 楼 WH_Always 2011-11-16  
 

相关推荐

Global site tag (gtag.js) - Google Analytics