View事件分发机制初步

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_DOWN
D/MyTextView(22257):  onTouchEvent ACTION_DOWN
D/MyRelativeLayout(22257):  onTouchEvent ACTION_DOWN
当把子View的onTouchEvent设置为false后,这时ViewGroup的onTouchEvent方法也会执行了。   




<2.2>ViewGroup的onTouchEvent设为true打印
D/MyRelativeLayout(23590):  onInterceptTouchEvent ACTION_DOWN
D/MyTextView(23590):  onTouchEvent ACTION_DOWN
D/MyRelativeLayout(23590):  onTouchEvent ACTION_DOWN
D/MyRelativeLayout(23590): onTouchEvent ACTION_MOVE

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值