测试android事件分发

一篇好文:http://blog.csdn.net/lmj623565791/article/details/39102591

先实现几个LinearLayout和Button 的继承类,加上日志:

MyLinearLayout:

package com.example.com.yzj.test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MyLinearLayout extends LinearLayout {
    String TAG = this.getClass().getSimpleName();

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context) {
        super(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MyLinearLayout-----dispatchTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MyLinearLayout-----dispatchTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MyLinearLayout-----dispatchTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MyLinearLayout-----dispatchTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MyLinearLayout-----onInterceptTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MyLinearLayout-----onInterceptTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MyLinearLayout-----onInterceptTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG,
                    "MyLinearLayout-----onInterceptTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MyLinearLayout-----onTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MyLinearLayout-----onTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MyLinearLayout-----onTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MyLinearLayout-----onTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onTouchEvent(ev);
    }
}

MyChildLinearLayout:

package com.example.com.yzj.test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class MyChildLinearLayout extends LinearLayout {
    String TAG = this.getClass().getSimpleName();

    public MyChildLinearLayout(Context context) {
        super(context);

    }

    public MyChildLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "ChildLinearLayout-----dispatchTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "ChildLinearLayout-----dispatchTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ChildLinearLayout-----dispatchTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG,
                    "ChildLinearLayout-----dispatchTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ChildLinearLayout-----onInterceptTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "ChildLinearLayout-----onTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "ChildLinearLayout-----onTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ChildLinearLayout-----onTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "ChildLinearLayout-----onTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onTouchEvent(ev);
    }
}

MyButton:

package com.example.com.yzj.test;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;

public class MyButton extends Button {

    String TAG=this.getClass().getSimpleName();

    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch(ev.getAction())
        {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MyButton-----dispatchTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MyButton-----dispatchTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MyButton-----dispatchTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MyButton-----dispatchTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch(ev.getAction())
        {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onTouchEvent(ev);
    }

}

MainActivity:

package com.example.com.yzj.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

public class MainActivity extends Activity {

    String TAG = this.getClass().getSimpleName();

    MyLinearLayout llayout_super;
    MyChildLinearLayout llayout_child;
    MyButton btn_my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        llayout_super = (MyLinearLayout) findViewById(R.id.llayout_super);
        llayout_child = (MyChildLinearLayout) findViewById(R.id.llayout_child);
        btn_my = (MyButton) findViewById(R.id.btn_my);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MainActivity-----dispatchTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MainActivity-----dispatchTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MainActivity-----dispatchTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MainActivity-----dispatchTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MainActivity-----onTouchEvent(ACTION_DOWN)");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MainActivity-----onTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MainActivity-----onTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MainActivity-----onTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onTouchEvent(ev);
    }
}

activity_main.xml

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <com.example.com.yzj.test.MyLinearLayout
        android:id="@+id/llayout_super"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <com.example.com.yzj.test.MyChildLinearLayout
            android:id="@+id/llayout_child"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:background="#ff0000"
            android:orientation="vertical" >

            <com.example.com.yzj.test.MyButton
                android:id="@+id/btn_my"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:background="#0000ff"
                android:text="llllll"
                android:textColor="#ffffff" >
            </com.example.com.yzj.test.MyButton>
        </com.example.com.yzj.test.MyChildLinearLayout>
    </com.example.com.yzj.test.MyLinearLayout>

</RelativeLayout>

展示效果:
这里写图片描述

一、事件传递
1.按上面的代码正常点击蓝色按钮:
MainActivity—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
MyButton—–dispatchTouchEvent(ACTION_DOWN)
MyButton—–onTouchEvent(ACTION_DOWN)
MainActivity—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–onInterceptTouchEvent(ACTION_UP)
ChildLinearLayout—–dispatchTouchEvent(ACTION_UP)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_UP)
MyButton—–dispatchTouchEvent(ACTION_UP)
MyButton—–onTouchEvent(ACTION_UP)

MyButton因为是可以clickable的,所以MyButton的onTouchEvent会返回true,消费掉该次事件

2.将activity_main.xml中的MyButton 的android:clickable属性设置为false,默认情况下Button是true的,点击蓝色按钮

MainActivity—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
MyButton—–dispatchTouchEvent(ACTION_DOWN)
MyButton—–onTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onTouchEvent(ACTION_DOWN)
MyLinearLayout—–onTouchEvent(ACTION_DOWN)
MainActivity—–onTouchEvent(ACTION_DOWN)
MainActivity—–dispatchTouchEvent(ACTION_UP)
MainActivity—–onTouchEvent(ACTION_UP)

因为MyButton的clickable为false,所以他的onTouchEvent没有消费事件,返回了false,所以依次往上冒泡,直到MainActivity,最终到Activity的onTouchEvent终止,所以在后面的up事件中,将不会再传递到子类再冒泡,而是直接由MainActivity处理up事件

3.在2的基础上将MyChildLinearLayout的onTouchEvent return true或者将activity_main.xml中child的android:clickable 属性设置为true,则点击蓝色按钮

MainActivity—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
MyButton—–dispatchTouchEvent(ACTION_DOWN)
MyButton—–onTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onTouchEvent(ACTION_DOWN)
MainActivity—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–onInterceptTouchEvent(ACTION_UP)
ChildLinearLayout—–dispatchTouchEvent(ACTION_UP)
ChildLinearLayout—–onTouchEvent(ACTION_UP)

从这里可以看出child的onTouchEvent消费了事件,同样在child的时候消费了down事件,在up的时候就从父到子child,不会再传到button

二、事件拦截
1.在原来最初的代码上将MyChildLinearLayout的onInterceptTouchEvent修改为

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_DOWN)");
            //break;
            return true;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ChildLinearLayout-----onInterceptTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onInterceptTouchEvent(ev);
    }

即拦截Down事件

则点击蓝色按钮
MainActivity—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onTouchEvent(ACTION_DOWN)
MyLinearLayout—–onTouchEvent(ACTION_DOWN)
MainActivity—–onTouchEvent(ACTION_DOWN)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_UP)
MainActivity—–onTouchEvent(ACTION_UP)

特意加了MOVE事件,可见button没有收到任何事件,全部被child拦截,至于后面的move,up就跟前面事件传递一样,因为child的onTouchEvent返回false,最终指向到了activity为止,则后面的move和up都在派发后直接给了activity来处理
这种情况下,down就被父拦击,则无论如何button都不会有事件了,无论用什么方法

2.在原来最初的代码上将MyChildLinearLayout的onInterceptTouchEvent修改为

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_DOWN)");
            break;          
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_MOVE)");
            //break;
            return true;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ChildLinearLayout-----onInterceptTouchEvent(ACTION_UP)");
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG,
                    "ChildLinearLayout-----onInterceptTouchEvent(ACTION_CANCEL)");
            break;
        }

        return super.onInterceptTouchEvent(ev);
    }

即拦截move事件

MainActivity—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
MyButton—–dispatchTouchEvent(ACTION_DOWN)
MyButton—–onTouchEvent(ACTION_DOWN)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–onInterceptTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_MOVE)
MyButton—–dispatchTouchEvent(ACTION_CANCEL)
MyButton—–onTouchEvent(ACTION_CANCEL)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–onInterceptTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–onTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–onInterceptTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–onTouchEvent(ACTION_MOVE)
MainActivity—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–onInterceptTouchEvent(ACTION_UP)
ChildLinearLayout—–dispatchTouchEvent(ACTION_UP)
ChildLinearLayout—–onTouchEvent(ACTION_UP)
MainActivity—–onTouchEvent(ACTION_UP)

在第一次移动的时候child中的move后,还会继续调用button的dispatch和ontouchevent,但是他们去执行的是action_cancel,猜想应该是前面有了down事件,现在要将其取消掉,在后面的move和up中不会再去执行,但是有个不解,为什么这样拦截后child的上层linearlayout中的ontouchevent没有执行呢? 猜想这就是因为move和up在viewgroup不能回传,直接到了activity的原因

至于这种情况下,如果button不让child拦截,是有办法的,因为他第一次down已经进去了,我们可以在button的down中做些设置,禁止他的父控件拦截事件

3.在2的基础上,修改MyButton代码:

@Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch(ev.getAction())
        {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_DOWN)");
            getParent().requestDisallowInterceptTouchEvent(true);
            break;
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_MOVE)");
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_UP)");
            getParent().requestDisallowInterceptTouchEvent(false);
            break;
        case MotionEvent.ACTION_CANCEL:
            Log.e(TAG, "MyButton-----onTouchEvent(ACTION_CANCEL)");
            getParent().requestDisallowInterceptTouchEvent(false);
            break;
        }

        return super.onTouchEvent(ev);
    }

事件顺序:
MainActivity—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
MyLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
ChildLinearLayout—–dispatchTouchEvent(ACTION_DOWN)
ChildLinearLayout—–onInterceptTouchEvent(ACTION_DOWN)
MyButton—–dispatchTouchEvent(ACTION_DOWN)
MyButton—–onTouchEvent(ACTION_DOWN)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_MOVE)
MyLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
ChildLinearLayout—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–dispatchTouchEvent(ACTION_MOVE)
MyButton—–onTouchEvent(ACTION_MOVE)
MainActivity—–dispatchTouchEvent(ACTION_UP)
MyLinearLayout—–dispatchTouchEvent(ACTION_UP)
ChildLinearLayout—–dispatchTouchEvent(ACTION_UP)
MyButton—–dispatchTouchEvent(ACTION_UP)
MyButton—–onTouchEvent(ACTION_UP)

可以看出child没能拦截button的事件,因为onInterceptTouchEvent根本没有执行,原因就是getParent().requestDisallowInterceptTouchEvent(true);禁止了父控件中的拦截,并且这个函数也是递归的,一致余从child到child的父控件的拦截函数都没有执行

这里面还有些疑问,还需要去探究。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值