AndroidTouch事件总结

一、自定义的控件几乎都要用到触摸事件,不交互怎么响应,相关的事件处理函数由dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent,处理这些事件的由view,viewGroup,和activityandroid

解释:虽然view能够dispatch和intercept,可是若是view是最小单元,那么就无法使用这两个方法了.函数

二、一个简单的activity,contentview以下布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

它的view层次以下所示:测试

三、对touch的派发规律spa

(1)interceptTouchEvent定义是否截取Touch消息,若在GroupView中想要处理Touch消息必须覆盖此方法3d

(2)消息在dispatch过程当中,若子view的dispatchTouchEvent返回true,父view再也不处理这个消息code

(3)viewGroup的interceptTouchEvent一但返回true,消息将再也不派发给子view。xml

四、实例测试:blog

(1)测试布局:事件

<com.example.testtouch.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/parent"
    tools:context=".MainActivity" >

    <com.example.testtouch.MyChildView
        android:id="@+id/child1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#0000ff" />
    
    <View 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#00ff00"/>
    
    
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"/>
</com.example.testtouch.MyRelativeLayout>

由此能够绘制出view树:

(2)对于dispatch事件:测试一:点击viewgroup:

测试二:点击MyChildView

测试三:在MyRelativeLayout中对dispatch事件返回true

测试四:在MyChildView中对dispatch返回true

对于dispatch事件:一直传递到最末端,若是

(3)对于onIntercept事件,测试一:MyRelativeLayout中对onIntercept事件返回false.

返回false后,当前控件再也不处理touch事件,调用子控件的dispatchTouch事件和子控件的touch事件

 

测试二:MyRelativeLayout中对onIntercept事件返回true.

返回true后,当前控件处理touch事件,并传给上一上一层处理,touch的传递就会结束

(4)对于onTouch事件:测试一,MyRelativeLayout中OnTouch何时处理,最子元素MyChildView调用super.onTouchEvent(event)时候,

能够看到,onTouch事件首先传到最子元素,若是子元素调用super.onTouchEvent(event),会传递到父元素,并一层层往上传

若是子元素的onTouch事件返回false

能够看到,若是最里层元素返回false,和上边的处理同样,就会调用父元素的onTouchEvent

若是子元素的onTouch事件返回true

返回true代表该元素对touch事件作了处理,父元素无需再处理,就再也不往上传了。

 

测试二:MyRelativeLayout对onInterceptTouchEvent返回true,对touch事件进行截获

对touch事件截获以后,touch事件会在当前层进行处理,再也不传忘子元素。

 若是截获后,当前层的onTouchEvent返回false

返回false后,自动调用上一层的onTouch事件。

若是截获后,当前层的OnTouch事件返回true

当前层返回true后,代表当前层不但愿父元素再进行处理touch事件,中断了touch事件往父元素的路由。

总结:ViewGroup层处理Touch事件的整体逻辑是:先检测是否须要拦截,没有拦截的话下发给子View处理,若是子View没有处理再自行处理,自行处理的逻辑与View同样。

相关文章
相关标签/搜索