Android ViewGroup 事件分发
2021-05-19 本文已影响0人
是刘航啊
上文已经分析了 View 的事件分发,View 事件分发的流程为 onTouch -> onTouchEvent -> onClick,而且一定会执行 dispatchTouchEvent 方法
下面还是通过例子来分析
public class TouchViewGroup extends RelativeLayout {
public TouchViewGroup(Context context) {
super(context);
}
public TouchViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("V10Activity", "onTouchEvent -> TouchViewGroup" + event.getAction());
return super.onTouchEvent(event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d("V10Activity", "dispatchTouchEvent -> TouchViewGroup" + event.getAction());
return super.dispatchTouchEvent(event);
}
}
在 Activity 中注册监听事件
v10Binding.touchGroup.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("V10Activity", "onTouch -> TouchGroup" + event.getAction());
return false;
}
});
v10Binding.btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("V10Activity", "btn1 onClick");
}
});
v10Binding.btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("V10Activity", "btn2 onClick");
}
});
-
点击 Button1
-
点击 Button2
-
点击空白区域
空白点击.png
虽然执行了 ViewGroup 的 dispatchTouchEvent 方法,但是并没有执行 onTouch 方法,所以咋们从 dispatchTouchEvent 源码分析
ViewGroup -> dispatchTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
...
//当执行 DOWN 事件会去清除重置 Touch 状态
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Throw away all previous state when starting a new touch gesture.
// The framework may have dropped the up or cancel event for the previous gesture
// due to an app switch, ANR, or some other state change.
cancelAndClearTouchTargets(ev);
resetTouchState();
}
final boolean intercepted;
//DOWN 会进行拦截
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
//onInterceptTouchEvent 默认返回 false,不拦截
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
//不拦截
intercepted = false;
}
} else {
intercepted = true;
}
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
//初始化新的 Touch 状态
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
//没有取消也没有拦截,则分发给子View
if (!canceled && !intercepted) {
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
//如果 Touch 状态为空并且子 View 个数不为 0
if (newTouchTarget == null && childrenCount != 0) {
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);
final ArrayList<View> preorderedList = buildTouchDispatchChildList();
final boolean customOrder = preorderedList == null
&& isChildrenDrawingOrderEnabled();
//循环查找
for (int i = childrenCount - 1; i >= 0; i--) {
//分发事件,记录状态
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
}
}
}
}
...
}
dispatchTransformedTouchEvent
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
...
if (child == null) {
handled = super.dispatchTouchEvent(transformedEvent);
} else {
final float offsetX = mScrollX - child.mLeft;
final float offsetY = mScrollY - child.mTop;
transformedEvent.offsetLocation(offsetX, offsetY);
if (! child.hasIdentityMatrix()) {
transformedEvent.transform(child.getInverseMatrix());
}
handled = child.dispatchTouchEvent(transformedEvent);
}
// Done.
transformedEvent.recycle();
return handled;
...
}
ViewGroup 的 dispatchTouchEven 首先会判断事件类型,如果为 DOWN 事件则不进行拦截,将事件分发给子 View。如果子View 为 0 则自己处理,否则通过循环便利。
所以我们修改 onInterceptTouchEvent 返回值
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
-
点击 Button1
-
点击 Button2
-
点击空白区域
最终都被 ViewGroup 消费了
ViewGroup 事件分发流程图
ViewGroup 事件分发总结
- 事件分发首先会调用 ViewGroup 的 dispatchTouchEvent 方法
- 接着会判断 onInterceptTouchEvent 的返回值
- 当 onInterceptTouchEvent 返回 false 时,会分发给子View,当 onInterceptTouchEvent 返回 true 时,会将事件拦截交给 ViewGroup 处理
ViewGroup 事件分发大致就介绍到这里了,如果有什么写得不对的,可以在下方评论留言,我会第一时间改正。