@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mInputEventConsistencyVerifier !=
null) {
mInputEventConsistencyVerifier.onTouchEvent(ev,
1);
}
if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) {
ev.setTargetAccessibilityFocus(
false);
}
boolean handled =
false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
resetTouchState();
}
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget !=
null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) !=
0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action);
}
else {
intercepted =
false;
}
}
else {
intercepted =
true;
}
if (intercepted || mFirstTouchTarget !=
null) {
ev.setTargetAccessibilityFocus(
false);
}
final boolean canceled = resetCancelNextUpFlag(
this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) !=
0;
TouchTarget newTouchTarget =
null;
boolean alreadyDispatchedToNewTouchTarget =
false;
if (!canceled && !intercepted) {
View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
? findChildWithAccessibilityFocus() :
null;
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
final int actionIndex = ev.getActionIndex();
final int idBitsToAssign = split ?
1 << ev.getPointerId(actionIndex)
: TouchTarget.ALL_POINTER_IDS;
removePointersFromTouchTargets(idBitsToAssign);
final int childrenCount = mChildrenCount;
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();
final View[] children = mChildren;
for (
int i = childrenCount -
1; i >=
0; i--) {
final int childIndex = getAndVerifyPreorderedIndex(
childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(
preorderedList, children, childIndex);
if (childWithAccessibilityFocus !=
null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus =
null;
i = childrenCount -
1;
}
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child,
null)) {
ev.setTargetAccessibilityFocus(
false);
continue;
}
newTouchTarget = getTouchTarget(child);
if (newTouchTarget !=
null) {
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
}
resetCancelNextUpFlag(child);
if (dispatchTransformedTouchEvent(ev,
false, child, idBitsToAssign)) {
mLastTouchDownTime = ev.getDownTime();
if (preorderedList !=
null) {
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;
}
ev.setTargetAccessibilityFocus(
false);
}
if (preorderedList !=
null) preorderedList.clear();
}
if (newTouchTarget ==
null && mFirstTouchTarget !=
null) {
给alreadyDispatchedToNewTouchTarget赋值为
true;
执行
break,因为该
for循环遍历子View判断哪个子View接受Touch事件,既然已经找到了就跳出该外层
for循环;
newTouchTarget = mFirstTouchTarget;
while (newTouchTarget.next !=
null) {
newTouchTarget = newTouchTarget.next;
}
newTouchTarget.pointerIdBits |= idBitsToAssign;
}
}
}
if (mFirstTouchTarget ==
null) {
handled = dispatchTransformedTouchEvent(ev, canceled,
null,
TouchTarget.ALL_POINTER_IDS);
}
else {
TouchTarget predecessor =
null;
TouchTarget target = mFirstTouchTarget;
while (target !=
null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled =
true;
}
else {
final boolean cancelChild = resetCancelNextUpFlag(target.child)
|| intercepted;
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled =
true;
}
if (cancelChild) {
if (predecessor ==
null) {
mFirstTouchTarget = next;
}
else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}
}
if (canceled
|| actionMasked == MotionEvent.ACTION_UP
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
resetTouchState();
}
else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {
final int actionIndex = ev.getActionIndex();
final int idBitsToRemove =
1 << ev.getPointerId(actionIndex);
removePointersFromTouchTargets(idBitsToRemove);
}
}
if (!handled && mInputEventConsistencyVerifier !=
null) {
mInputEventConsistencyVerifier.onUnhandledEvent(ev,
1);
}
return handled;
}
/**
* 运动事件被分配下来
*如果返回true的话,那么它会截获子控件的motionEvent,将其通过TouchEvent分发给这个ViewGroup
*当前目标将收到一个ACTION_CANCEL事件,此处将不会传送进一步的消息。
*/
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
&& ev.getAction() == MotionEvent.ACTION_DOWN
&& ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
&& isOnScrollbarThumb(ev.getX(), ev.getY())) {
return true;
}
return false;
}
public void setMotionEventSplittingEnabled(
boolean split) {
if (split) {
mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
}
else {
mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS;
}
}