为了研究ViewGroup和View中onTouchEvent和onInterceptTouchEvent中事件的传递和分发机制,写了个如下demo进行测试。
准备:两个ViewGroup和一个View,其中两个ViewGroup一层层包含view
如下是一个ViewGroup和View,另外一个ViewGroup一样的,
package guo.com.ontoucheventtest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
/**
* Created by ${GuoZhaoHui} on 2017/4/28.
* email:[email protected]
*/
public class LL1 extends LinearLayout {
private static final String TAG = "LL1";
public LL1(Context context) {
super(context);
}
public LL1(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LL1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "--------down--------------");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "--------move--------------");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "--------up--------------");
break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "--------cancle--------------");
break;
}
return false;
}
}
package guo.com.ontoucheventtest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;
/**
* Created by ${GuoZhaoHui} on 2017/4/28.
* email:[email protected]
*/
public class TV extends TextView {
private static final String TAG = "TV";
public TV(Context context) {
super(context);
}
public TV(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TV(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "--------down--------------");
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "--------move--------------");
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "--------up--------------");
break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "--------cancle--------------");
break;
}
return true;
}
}
xml如下
<?xml version="1.0" encoding="utf-8"?>
<guo.com.ontoucheventtest.LL1 xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="guo.com.ontoucheventtest.MainActivity">
<guo.com.ontoucheventtest.LL2
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<guo.com.ontoucheventtest.TV
android:textSize="25sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</guo.com.ontoucheventtest.LL2>
</guo.com.ontoucheventtest.LL1>
分情况
1:ViewGroup和View的onTouchEvent都是返回false,且并不重写onInterceptTouchEvent
点击view或者ViewGroup,得到如下结果
点击view
/guo.com.ontoucheventtest D/TV: --------down--------------
/guo.com.ontoucheventtest D/LL2: ------