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

解释:虽然view可以dispatch和intercept,但是如果view是最小单元,那么就没法使用这两个方法了.

2、一个简单的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层次如下所示:

3、对touch的派发规律

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

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

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

4、实例测试:

(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一样。

posted on 2015-08-15 16:24  猿类的进化史  阅读(447)  评论(0编辑  收藏  举报