布局很简单,代码就是打个日志看看,为了不影响阅读,放在最后
Android事件处理机制真是个磨人的小妖精,被她卡过两次,一卡住就不行玩安卓,跑过去玩html、js、css了
最好自己动手测试一下,印象更深
一、妖鸟三姐妹:dispatchTouchEvent---onInterceptTouchEvent---onTouchEvent
点击:ViewGroup1(最外层)
I/System.out: MainActivity dispatchTouchEvent
I/System.out: ViewGroup1 dispatchTouchEvent
ViewGroup1 onInterceptTouchEvent
I/System.out: ViewGroup1 onTouchEvent
MainActivity onTouchEvent
I/System.out: MainActivity dispatchTouchEvent
MainActivity onTouchEvent
点击ViewGroup 1.png
点击:ViewGroup2(中间层)
I/System.out: MainActivity dispatchTouchEvent
ViewGroup1 dispatchTouchEvent
ViewGroup1 onInterceptTouchEvent
ViewGroup2 dispatchTouchEvent
ViewGroup2 onInterceptTouchEvent
ViewGroup2 onTouchEvent
ViewGroup1 onTouchEvent
MainActivity onTouchEvent
I/System.out: MainActivity dispatchTouchEvent
MainActivity onTouchEvent
点击ViewGroup 2.png
点击:View1
I/System.out: MainActivity dispatchTouchEvent
I/System.out: ViewGroup1 dispatchTouchEvent
ViewGroup1 onInterceptTouchEvent
ViewGroup2 dispatchTouchEvent
ViewGroup2 onInterceptTouchEvent
View1 dispatchTouchEvent
View1 onTouchEvent
ViewGroup2 onTouchEvent
ViewGroup1 onTouchEvent
MainActivity onTouchEvent
I/System.out: MainActivity dispatchTouchEvent
MainActivity onTouchEvent
点击View.png
点击ViewGroup.png苹果树上掉下(分发:dispatchTouchEvent false)个苹果(event),爷爷(ViewGroup1)拿到。
爷爷如果不分发(dispatchTouchEvent true)了,相当于把苹果扔了,谁都不吃,包括他自己
他如果截断(onInterceptTouchEvent true),自己可以把苹果吃了(执行onTouchEvent 并返回true),也可以不吃(flase),但只要截断,就不会再向下传了。
dispatchTouchEvent:决定了事件是否继续分发下去和是否响应事件
false:继续分发,
true:不继续分发--此次事件到此结束,也不会有任何控件执行onTouchEvent方法。
onInterceptTouchEvent:决定了是否拦截该事件
false:不拦截
true:拦截---此时当前控件执行onTouchEvent方法。
onTouchEvent:决定了是否消费该事件
false:不消费
true:消费。
二、事件处理机制对view.setOnClickListener的影响
对于View1来说:setOnClickListener:
能执行必须前面的分发,不截断,畅通无阻, 并且自己的onTouchEvent返回super.onTouchEvent(event),有一个环节出现意外都无法触发
对于ViewGroup2(也就是View1的父容器),所有均默认,如果View1的onTouchEvent返回false
则点击View1的时候ViewGroup2的setOnClickListener会触发。
所以来看setOnClickListener的触发条件还是蛮苛刻的。
三、View的几个生命函数的调用顺序
ViewGroup.png用一个ViewGroup3,包裹View2和View3两个View,进行测试,详情看图
四、代码:
1.事件测试部分
View1
public class View1 extends View {
public View1(Context context) {
super(context);
}
public View1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View1(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
System.out.println(getClass().getSimpleName() + " dispatchTouchEvent:" );
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println(getClass().getSimpleName() + " onTouchEvent:" );
return super.onTouchEvent(event);
}
}
ViewGroup1
public class ViewGroup1 extends LinearLayout {
public ViewGroup1(Context context) {
super(context);
}
public ViewGroup1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ViewGroup1(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
System.out.println(getClass().getSimpleName() + " dispatchTouchEvent:" );
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
System.out.println(getClass().getSimpleName() + " onInterceptTouchEvent:");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println(getClass().getSimpleName() + " onTouchEvent:" );
return super.onTouchEvent(event);
}
}
ViewGroup2
public class ViewGroup2 extends LinearLayout {
public ViewGroup2(Context context) {
super(context);
}
public ViewGroup2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ViewGroup2(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
System.out.println(getClass().getSimpleName() + " dispatchTouchEvent:" );
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
System.out.println(getClass().getSimpleName() + " onInterceptTouchEvent:" );
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println(getClass().getSimpleName() + " onTouchEvent:");
return super.onTouchEvent(event);
}
}
布局:
<com.toly1994.d.event.ViewGroup1
android:id="@+id/vg1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<com.toly1994.d.event.ViewGroup2
android:id="@+id/vg2"
android:layout_width="@dimen/dp_200"
android:layout_height="@dimen/dp_200"
android:background="@android:color/darker_gray"
android:orientation="vertical"
android:padding="20dp">
<com.toly1994.d.event.View1
android:id="@+id/v1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/white"
android:gravity="center"
android:padding="20dp"
android:text="me is textview"/>
</com.toly1994.d.event.ViewGroup2>
</com.toly1994.d.event.ViewGroup1>
2.生命函数测试
View2
public class View2 extends View {
public View2(Context context) {
super(context);
}
public View2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View2(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println(getClass().getSimpleName() + " onMeasure:" );
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
System.out.println(getClass().getSimpleName() + " onSizeChanged:" );
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
System.out.println(getClass().getSimpleName() + " onLayout:" );
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
System.out.println(getClass().getSimpleName() + " onFinishInflate:" );
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println(getClass().getSimpleName() + " onDraw:" );
}
}
View3
public class View3 extends View {
public View3(Context context) {
super(context);
}
public View3(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View3(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println(getClass().getSimpleName() + " onMeasure:" );
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
System.out.println(getClass().getSimpleName() + " onSizeChanged:" );
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
System.out.println(getClass().getSimpleName() + " onLayout:" );
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
System.out.println(getClass().getSimpleName() + " onFinishInflate:" );
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println(getClass().getSimpleName() + " onDraw:" );
}
}
ViewGroup3
public class ViewGroup3 extends LinearLayout {
public ViewGroup3(Context context) {
super(context);
}
public ViewGroup3(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ViewGroup3(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
System.out.println(getClass().getSimpleName() + " onMeasure:");
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
System.out.println(getClass().getSimpleName() + " onFinishInflate:");
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
System.out.println(getClass().getSimpleName() + " onSizeChanged:");
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
System.out.println(getClass().getSimpleName() + " onLayout:");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.out.println(getClass().getSimpleName() + " onDraw:");
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<com.toly1994.d.event.ViewGroup3
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/bisque"
android:orientation="horizontal">
<com.toly1994.d.event.View2
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/blue_dark"/>
<com.toly1994.d.event.View3
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/darkkhaki"/>
</com.toly1994.d.event.ViewGroup3>
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328
网友评论