View事件分发机制初步

来源:互联网 发布:黑魂3剧情解析 知乎 编辑:程序博客网 时间:2024/06/05 03:13

source code

package com.example.viewandviewgroup;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.RelativeLayout;public class MyRelativeLayout extends RelativeLayout {    private static  final String TAG="MyRelativeLayout";     public MyRelativeLayout(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        // TODO Auto-generated method stub        int action=ev.getAction();        switch (action) {        case MotionEvent.ACTION_DOWN:        {            Log.d(TAG," onInterceptTouchEvent ACTION_DOWN");        }            break;        case MotionEvent.ACTION_MOVE:        {            Log.d(TAG,"onInterceptTouchEvent ACTION_MOVE");        }            break;        case MotionEvent.ACTION_UP:        {            Log.d(TAG,"onInterceptTouchEvent ACTION_UP");        }            break;        default:            break;        }        return true;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        // TODO Auto-generated method stub        int action=event.getAction();        switch (action) {        case MotionEvent.ACTION_DOWN:        {            Log.d(TAG," onTouchEvent ACTION_DOWN");        }            break;        case MotionEvent.ACTION_MOVE:        {            Log.d(TAG,"onTouchEvent ACTION_MOVE");        }            break;        case MotionEvent.ACTION_UP:        {            Log.d(TAG,"onTouchEvent ACTION_UP");        }            break;        default:            break;        }        return super.onTouchEvent(event);    }}
package com.example.viewandviewgroup;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.TextView;public class MyTextView extends TextView {    private static  final String TAG="MyTextView";     public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }     @Override    public boolean onTouchEvent(MotionEvent event) {         int action=event.getAction();            switch (action) {            case MotionEvent.ACTION_DOWN:            {                Log.d(TAG," onTouchEvent ACTION_DOWN");            }                break;            case MotionEvent.ACTION_MOVE:            {                Log.d(TAG,"onTouchEvent ACTION_MOVE");            }                break;            case MotionEvent.ACTION_UP:            {                Log.d(TAG,"onTouchEvent ACTION_UP");            }                break;            default:                break;            }            return super.onTouchEvent(event);    }}
<com.example.viewandviewgroup.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"    tools:context=".MainActivity" >    <com.example.viewandviewgroup.MyTextView        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="MyTextView"        /></com.example.viewandviewgroup.MyRelativeLayout>

代码还是比较清晰的,自定义的两个View,点击MyTextView,追踪一下关日子打印。

情形一

如上所示
ViewGroup的onInterceptTouchEvent返回true。ViewGroup和View的onTouchEvent保持默认。
打印如下:

D/MyRelativeLayout(15136):  onInterceptTouchEvent ACTION_DOWN D/MyRelativeLayout(15136):  onTouchEvent ACTION_DOWN

当onInterceptTouchEvent 返回为true,子view的onTouchEvent不会执行。同时当前ViewGroup的onTouchEvent还是能执行的。
总结:如果返回true的话证明viewgroup消费了此手势的一系列事触摸事件,子view的onTouchEvent事件不能执行。

情形二:

当onInterceptTouchEvent 返回为false,即修改ViewGroup onInterceptTouchEvent 的最后一句话。
在这个大前提下,在细分。
<1>、子view的onTouchEvent为true.

 D/MyRelativeLayout(20449):  onInterceptTouchEvent ACTION_DOWN D/MyTextView(20449):  onTouchEvent ACTION_DOWN

ViewGroup的 onTouchEvent 没有执行。子view的onTouchEvent执行了。

<2>子View的onTouchEvent为false

 <2.1> ViewGroup的onTouchEvent也为false,  D/MyRelativeLayout(22257):  onInterceptTouchEvent ACTION_DOWND/MyTextView(22257):  onTouchEvent ACTION_DOWND/MyRelativeLayout(22257):  onTouchEvent ACTION_DOWN当把子View的onTouchEvent设置为false后,这时ViewGroup的onTouchEvent方法也会执行了。   <2.2>ViewGroup的onTouchEvent设为true打印
D/MyRelativeLayout(23590):  onInterceptTouchEvent ACTION_DOWND/MyTextView(23590):  onTouchEvent ACTION_DOWND/MyRelativeLayout(23590):  onTouchEvent ACTION_DOWND/MyRelativeLayout(23590): onTouchEvent ACTION_MOVE

相比较于上一个Actionmove和Action up也会调用。

把几种情况记下来,不至于混淆。

0 0
原创粉丝点击