思路:override相关类里面的touch事件方法,通过修改返回值来看实际的机制。
最外层Activity
package com.sohu.androidtouchtest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("test", getClass().getCanonicalName() + "dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override public boolean onTouchEvent(MotionEvent event) {
Log.e("test", getClass().getCanonicalName() + "dispatchTouchEvent");
return false;
}
}
父布局TestViewGroup1
package com.sohu.androidtouchtest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
/**
* Created by lucky-django on 16/6/14.
*/
public class TestViewGroup1 extends RelativeLayout {
public TestViewGroup1(Context context) {
super(context);
}
public TestViewGroup1(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestViewGroup1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("test", getClass().getCanonicalName() + "dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.e("test", getClass().getCanonicalName() + "onInterceptTouchEvent");
return false;
}
@Override public boolean onTouchEvent(MotionEvent event) {
Log.e("test", getClass().getCanonicalName() + "onTouchEvent");
return false;
}
}
子布局TestViewGroup2类似
子View
package com.sohu.androidtouchtest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by lucky-django on 16/6/14.
*/
public class TestView extends TextView {
public TestView(Context context) {
super(context);
}
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("test", getClass().getCanonicalName() + "dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override public boolean onTouchEvent(MotionEvent event) {
Log.e("test", getClass().getName() + "onTouchEvent");
return false;
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<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="com.sohu.androidtouchtest.MainActivity">
<com.sohu.androidtouchtest.TestViewGroup1 android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp">
<com.sohu.androidtouchtest.TestViewGroup2 android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp">
<com.sohu.androidtouchtest.TestView android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text View"
android:textSize="50dp"/>
</com.sohu.androidtouchtest.TestViewGroup2>
</com.sohu.androidtouchtest.TestViewGroup1>
</RelativeLayout>
下面开始测试,点击中间的子view,结果如下
在所有地方都不捕获事件时,流程为
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
5.Layout2.intercept
6.View.dispatch
7.View.onTouchEvent
8.Layout2.onTouchEvent
9.Layout1.onTouchEvent
10.Activity.onTouchEvent
而且,由于ACTION_DOWN事件没有被捕获,后续的ACTION_MOVE,和ACTION_UP事件直接传递到Actitivy的onTouchEvent上。
现在把TestViewGroup2的onInterceptTouchEvent返回true,结果为
这时候事件没有向下一级TestView传递了,而由于这个事件仍然没有被消费掉,后续的事件还是只传递到了Activity
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
5.Layout2.intercept
8.Layout2.onTouchEvent
9.Layout1.onTouchEvent
10.Activity.onTouchEvent
现在让TestViewGroup2拦截,而且把事件消费掉,即onTouchEvent也返回true
ACTION_DOWN
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
5.Layout2.intercept
8.Layout2.onTouchEvent
ACTION_MOVE
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
8.Layout2.onTouchEvent
ACTION_UP
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
8.Layout2.onTouchEvent
现在让TestViewGroup2 intercept,不消费,而TestViewGroup1消费掉。
ACTION_DOWN
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
5.Layout2.intercept
8.Layout2.onTouchEvent
9.Layout1.onTouchEvent
ACTION_MOVE
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.onTouchEvent
ACTION_UP
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.onTouchEvent
接下来把一切还原,放个大招,在TestView的dispatchTouchEvent调用requestDisallowInterceptTouchEvent试一下。
@Override public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("test", getClass().getCanonicalName() + "dispatchTouchEvent");
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.dispatchTouchEvent(ev);
}
没什么卵用,因为并没有在onTouchEvent中捕获ACTION_DOWN事件,后续事件根本就不传进来,接下来捕获一下这个事件。
@Override public boolean onTouchEvent(MotionEvent event) {
Log.e("test", getClass().getName() + "onTouchEvent");
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
}
return false;
}
这就有意思了
ACTION_DOWN
1.Activity.dispatch
2.Layout1.dispatch
3.Layout1.intercept
4.Layout2.dispatch
5.Layout2.intercept
6.View.dispatch
7.View.onTouch
ACTION_MOVE
1.Activity.dispatch
2.Layout1.dispatch
3.Layout2.dispatch
4.View.onTouch
5.Activity.onTouch
ACTION_UP
1.Activity.dispatch
2.Layout1.dispatch
3.Layout2.dispatch
4.View.onTouch
5.Activity.onTouch
说明在同一个事件序列中,调用了requestDisallowInterceptTouchEvent,父布局接下来的事件不会再走onInterceptTouchEvent
而只要捕获了第一个事件,后续的事件也会传递到View这一层。