Android Touch事件之一:Touch事件在父ViewGroup和子View之间的传递篇

时间:2023-03-09 16:55:42
Android Touch事件之一:Touch事件在父ViewGroup和子View之间的传递篇

2015-11-26 17:00:22

前言:Android的Touch事件传递和View的实现紧密相连,因此理解Touch事件的传递,有助于我们更好的理解View的工作原理。

1. 几个重要的方法:

View.java

=============

dispatchTouchEvent():用来分发、传递Touch事件,如果Touch事件被当前View处理了,就返回true,否则返回false。

     /**
* Pass the touch screen motion event down to the target view, or this
* view if it is the target.
* 将Touch事件传递到目标View,如果自己就是目标View的话,就给自己
* @param event The motion event to be dispatched.
* @return True if the event was handled by the view, false otherwise.
*/
public boolean dispatchTouchEvent(MotionEvent event) {
// If the event should be handled by accessibility focus first.
if (event.isTargetAccessibilityFocus()) {
// We don't have focus or no virtual descendant has it, do not handle the event.
if (!isAccessibilityFocusedViewOrHost()) {
return false;
}
// We have focus and got the event, then use normal event dispatch.
event.setTargetAccessibilityFocus(false);
} boolean result = false; if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
} final int actionMasked = event.getActionMasked();
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Defensive cleanup for new gesture
stopNestedScroll();
} if (onFilterTouchEventForSecurity(event)) {
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) { (1)
result = true;
} if (!result && onTouchEvent(event)) { (2)
result = true;
}
} if (!result && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
} // Clean up after nested scrolls if this is the end of a gesture;
// also cancel it if we tried an ACTION_DOWN but we didn't want the rest
// of the gesture.
if (actionMasked == MotionEvent.ACTION_UP ||
actionMasked == MotionEvent.ACTION_CANCEL ||
(actionMasked == MotionEvent.ACTION_DOWN && !result)) {
stopNestedScroll();
} return result;
}

通俗理解:

该方法是View上Touch事件传递的第一道防线,现在只需要记住:

(1)处代码调用了OnTouchListener,如果你给当前View设置了setOnTouchListener(),那么,此处会被调用;

(2)如果你设置的onTouchListener的onTouch方法,返回的是false,那么此处有机会调用当前View的onTouchEvent方法。如果返回true,不会调用onTouchEvent,同时你设置的onClickListener也不会得到调用,这点我们接下来分析。

onTouchEvent():当前View收到Touch事件后,真正使用Touch事件的地方。

     public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
final int viewFlags = mViewFlags; if ((viewFlags & ENABLED_MASK) == DISABLED) {
if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {
setPressed(false);
}
// A disabled view that is clickable still consumes the touch
// events, it just doesn't respond to them.
return (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
} if (mTouchDelegate != null) {
if (mTouchDelegate.onTouchEvent(event)) {
return true;
}
} if (((viewFlags & CLICKABLE) == CLICKABLE ||
(viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;
if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {
// take focus if we don't have it already and we should in
// touch mode.
boolean focusTaken = false;
if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
focusTaken = requestFocus();
} if (prepressed) {
// The button is being released before we actually
// showed it as pressed. Make it show the pressed
// state now (before scheduling the click) to ensure
// the user sees it.
setPressed(true, x, y);
} if (!mHasPerformedLongPress) {
// This is a tap, so remove the longpress check
removeLongPressCallback(); // Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick(); (1)
}
}
} if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();
} if (prepressed) {
postDelayed(mUnsetPressedState,
ViewConfiguration.getPressedStateDuration());
} else if (!post(mUnsetPressedState)) {
// If the post failed, unpress right now
mUnsetPressedState.run();
} removeTapCallback();
}
break; case MotionEvent.ACTION_DOWN:
mHasPerformedLongPress = false; if (performButtonActionOnTouchDown(event)) {
break;
} // Walk up the hierarchy to determine if we're inside a scrolling container.
boolean isInScrollingContainer = isInScrollingContainer(); // For views inside a scrolling container, delay the pressed feedback for
// a short period in case this is a scroll.
if (isInScrollingContainer) {
mPrivateFlags |= PFLAG_PREPRESSED;
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPendingCheckForTap.x = event.getX();
mPendingCheckForTap.y = event.getY();
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
} else {
// Not inside a scrolling container, so show the feedback right away
setPressed(true, x, y);
checkForLongClick(0); (2)
}
break; case MotionEvent.ACTION_CANCEL:
setPressed(false);
removeTapCallback();
removeLongPressCallback();
break; case MotionEvent.ACTION_MOVE:
drawableHotspotChanged(x, y); // Be lenient about moving outside of buttons
if (!pointInView(x, y, mTouchSlop)) {
// Outside button
removeTapCallback();
if ((mPrivateFlags & PFLAG_PRESSED) != 0) {
// Remove any future long press/tap checks
removeLongPressCallback(); setPressed(false);
}
}
break;
} return true;
} return false;
}

在onTouchEvent()方法中,分别处理不同类型的Touch事件。注意:

(1)当前View处理Click事件的地方,没错,就是在MotionEvent.Action_UP的时候处理的;

(2)当前View处理LongClick事件的地方,在MotionEvent.Action_DOWN的时候处理的;

ViewGroup.java

=============

ViewGroup是继承自View的,当然也继承了上面那两个方法。ViewGroup不仅是一个View,也是一个可以拥有Children View的View Container,所以它重写了dispatchTouchEvent()方法,但是没有重写onTouchEvent()方法。同时,ViewGroup觉得自己好歹是个Container,得有点特权嘛,所以给自己搞了个onInterceptTouchEvent()方法,如下:

     public boolean dispatchTouchEvent(MotionEvent ev) {

         boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK; // Handle an initial down.
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();
} // Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev); (1)
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
} // Check for cancelation.
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL; // Update list of touch targets for pointer down, if needed.
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
if (!canceled && !intercepted) { (2)
…… }

ViewGroup的dispatchTouchView()方法,加了一些自己的判断,最终调用的仍然是View中的dispatchTouchView(),先关注一下红色代码:

(1)ViewGroup调用了自己的onInterceptTouchEvent();

(2)只有onInterceptTouchEvent()返回false时,才会继续执行下面的代码。这里可以这么理解,onInterceptTouchEvent()返回true,表示该Touch事件被拦截,不会向Children View传递,分发给自己handle,如果返回false,才会继续向Children View传递。

onInterceptTouchEvent(MotionEvent ev):

     /* @param ev The motion event being dispatched down the hierarchy.
* @return Return true to steal motion events from the children and have
* them dispatched to this ViewGroup through onTouchEvent().
* The current target will receive an ACTION_CANCEL event, and no further
* messages will be delivered here.
*/
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}

此方法默认返回false。

2. 写个小Demo,源码如下:

MyLinear1.java

 public class MyLinear1 extends LinearLayout {
public MyLinear1(Context context) {
super(context);
} public MyLinear1(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyLinear1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
} boolean isDispatch = true; @Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.e("David--MyLinear", "dispatchTouchEvent-" + getS(ev.getAction()));
if (isDispatch) {
boolean one = super.dispatchTouchEvent(ev);
Log.e("David--MyLinear", "dispatchTouchEvent() super.dispatchTouchEvent(event) = " + one);
return one;
} else {
return true;
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("David--MyLinear", "onTouchEvent-" + getS(event.getAction()));
return true;
} public static String getS(int action) {
String ret = "null";
switch (action) {
case MotionEvent.ACTION_CANCEL:
ret = "Cancel";
break;
case MotionEvent.ACTION_DOWN:
ret = "Down";
break;
case MotionEvent.ACTION_MOVE:
ret = "Move";
break;
case MotionEvent.ACTION_UP:
ret = "Up";
break;
}
return ret;
}
}

继承自LinearLayout,重写了那几个方法。

CustomView.java

 public class CustomView extends View {
private Paint mPaint; public CustomView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.RED);
} public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.RED);
} public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setColor(Color.RED);
} @Override
public void draw(Canvas canvas) {
super.draw(canvas);
//canvas.drawLine(0, 0, 100, 400, mPaint);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);
mPaint.setColor(Color.GREEN);
canvas.drawText("我是文字", 30, 45, mPaint);
canvas.restore();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getContext().getResources().getDisplayMetrics().widthPixels, 400);
} @Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.e("David--C---------------", "dispatchTouchEvent-" + MyLinear1.getS(event.getAction()));
boolean one = super.dispatchTouchEvent(event);
Log.e("David--C---------------", "dispatchTouchEvent() super.dispatchTouchEvent(event) = " + one);
return one;
} @Override
public boolean onTouchEvent(MotionEvent event) {
Log.e("David--C---------------", "onTouchEvent-" + MyLinear1.getS(event.getAction()));
return false;
}
}

这是一个简单地View。

布局文件:当前页面的根View可以认为是MyLinear1,里面只包含了一个子CustomView。至于Activity的根View,我们后续再分析。

 <?xml version="1.0" encoding="utf-8"?>
<com.meituan.david.myapplication.MyLinear1 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.meituan.david.myapplication.CustomView
android:layout_width="match_parent"
android:layout_height="200dp" />
</com.meituan.david.myapplication.MyLinear1>

上日志:

前提:MyLinear1的onInterceptTouchEvent()方法返回true,意味着Touch事件不会向Children View传递,所以CustomView的两个方法的返回值无所谓了~

1. MyLinear1的dispatchTouchEvent()方法返回false,onTouchEvent返回false,日志如下:
 11-30 18:12:01.014 E/David--MyLinear(20933): dispatchTouchEvent-Down
11-30 18:12:02.139 E/David--MyLinear(20933): dispatchTouchEvent-Down
11-30 18:12:03.004 E/David--MyLinear(20933): dispatchTouchEvent-Down

触摸(点击、滑动)操作三次打出的log,事件根本没有传递到子View,并且,由于自己MyLinear1不想处理Touch事件,所以只收到了ACTION_DOWN,此后的Touch事件不会再传入了;

2. MyLinear1的dispatchTouchEvent()方法返回false,onTouchEvent返回true,日志如下:

日志和1是一样的,很明显,如果MyLinear1的dispatchTouchEvent()直接返回false,无论其他方法的返回值如何,说明当前ViewGroup不想处理Touch事件,所有后续的Touch事件不会再传入了。

3. MyLinear1的dispatchTouchEvent()方法返回true,onTouchEvent返回false,日志如下:

11-30 18:26:38.451 E/David--MyLinear( 8268): dispatchTouchEvent-Down
11-30 18:26:38.592 E/David--MyLinear( 8268): dispatchTouchEvent-Move
11-30 18:26:38.608 E/David--MyLinear( 8268): dispatchTouchEvent-Move
11-30 18:26:38.625 E/David--MyLinear( 8268): dispatchTouchEvent-Move
11-30 18:26:38.798 E/David--MyLinear( 8268): dispatchTouchEvent-Up

MyLinear1的dispatchTouchEvent()直接返回true,Touch事件源源不断的传入,但是onInterceptTouchEvent()和onTouchEvent()并没有被调用。

根据以上三点,我们可以得出一个结论:

MyLinear1的dispatchTouchEvent()方法,会接受"系统"(暂且认为是系统吧)传入的Touch事件,如果直接返回false,则系统不会再传入Touch事件;返回true,会继续传入。但是,由于没有调用super.dispatchTouchEvent()方法,此Touch事件既不会被拦截/分发,也不会被处理(onInterceptTouchEvent()和onTouchEvent()不会被调用)~

4. MyLinear1的dispatchTouchEvent()方法返回super.dispatchTouchEvent(),onTouchEvent返回false,日志如下:

 11-30 18:56:04.757 E/David--MyLinear( 8156): dispatchTouchEvent-Down
11-30 18:56:04.758 E/David--MyLinear( 8156): onInterceptTouchEvent-Down
11-30 18:56:04.758 E/David--MyLinear( 8156): onTouchEvent-Down
11-30 18:56:04.758 E/David--MyLinear( 8156): dispatchTouchEvent() super.dispatchTouchEvent(event) = false

另外两个方法得到了调用,但是由于onTouchEvent返回false,说明MyLinear1不想处理后续的Touch事件,所以后续的Touch事件没有传入。仔细看,此时的super.dispatchTouchEvent(event) = false,这也解释了为什么只传入了ACTION_DOWN事件。

5. MyLinear1的dispatchTouchEvent()方法返回super.dispatchTouchEvent(),onTouchEvent返回true,日志如下:

 11-30 19:02:18.189 E/David--MyLinear(11377): dispatchTouchEvent-Down
11-30 19:02:18.189 E/David--MyLinear(11377): onInterceptTouchEvent-Down
11-30 19:02:18.189 E/David--MyLinear(11377): onTouchEvent-Down
11-30 19:02:18.476 E/David--MyLinear(11377): dispatchTouchEvent() super.dispatchTouchEvent(event)-true 11-30 19:02:18.505 E/David--MyLinear(11377): dispatchTouchEvent-Move
11-30 19:02:18.505 E/David--MyLinear(11377): onTouchEvent-Move
11-30 19:02:18.505 E/David--MyLinear(11377): dispatchTouchEvent() super.dispatchTouchEvent(event)-true 11-30 19:02:18.505 E/David--MyLinear(11377): dispatchTouchEvent-Up
11-30 19:02:18.505 E/David--MyLinear(11377): onTouchEvent-Up
11-30 19:02:18.505 E/David--MyLinear(11377): dispatchTouchEvent() super.dispatchTouchEvent(event)-true

现在onTouchEvent返回true,说明他愿意处理Touch事件,所以就源源不断的传入了,同时super.dispatchTouchEvent(event)=true,接受系统传入的Touch事件~同时呢,如果当前View决定处理=Touch事件,那么后续传入的Touch事件,就不需要onInterceptTouchEvent了....

根据4、5,我们可以得出一个结论:

dispatchTouchEvent()的返回值和onTouchEvent()的返回值是息息相关的,所以diapatchTouchEvent方法不调用super.dispatchTouchEvent()是没有任何意义的。

以上都是在onInterceptTouchEvent()方法返回true的情况下做的测试,Touch事件根本就没有传递到Children View,那么我们看看onInterceptTouchEvent()方法返回false时,会有什么样的结果。

前提:MyLinear1的dispatchTouchEvent()方法返回super.dispatchTouchEvent(),onInterceptTouchEvent()方法返回false,表示不拦截Touch事件

1. CustomView的dispatchTouchEvent()返回super.dispatchTouchEvent,onTouchEvent返回false,MyLinear1的onTouchEvent返回true,日志如下:

 11-30 19:19:54.430 E/David--MyLinear( 1860): dispatchTouchEvent-Down
11-30 19:19:54.430 E/David--MyLinear( 1860): onInterceptTouchEvent-Down
11-30 19:19:54.430 E/David--C---------------( 1860): dispatchTouchEvent-Down
11-30 19:19:54.430 E/David--C---------------( 1860): onTouchEvent-Down
11-30 19:19:54.430 E/David--C---------------( 1860): dispatchTouchEvent() super.dispatchTouchEvent(event) = false
11-30 19:19:54.430 E/David--MyLinear( 1860): onTouchEvent-Down 11-30 19:19:54.659 E/David--MyLinear( 1860): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
11-30 19:19:54.680 E/David--MyLinear( 1860): dispatchTouchEvent-Move
11-30 19:19:54.680 E/David--MyLinear( 1860): onTouchEvent-Move
11-30 19:19:54.680 E/David--MyLinear( 1860): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
11-30 19:19:54.794 E/David--MyLinear( 1860): dispatchTouchEvent-Move
11-30 19:19:54.794 E/David--MyLinear( 1860): onTouchEvent-Move 11-30 19:19:54.794 E/David--MyLinear( 1860): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
11-30 19:19:54.825 E/David--MyLinear( 1860): dispatchTouchEvent-Up
11-30 19:19:54.825 E/David--MyLinear( 1860): onTouchEvent-Up
11-30 19:19:54.825 E/David--MyLinear( 1860): dispatchTouchEvent() super.dispatchTouchEvent(event) = true

事件顺利的传到Custom View,但是由于CustomView的onTouchEvent返回false,意味着他不想处理后续Touch事件,所以,CustomView的dispatchTouchEvent()返回false,Touch事件不再传入到CustomView。那么传入的ACTION_DOWN和后续的Touch事件怎么办呢,总不能没人管吧?其实,当Child View不想处理ACTION_DOWN事件时,ACTION_DOWN就会被回传至Father View,很显然,CustomView的父View就是MyLinear1了,所以MyLinear1的onTouchEvent收到了该事件。如果MyLinear的onTouchEvent决定处理Touch事件,那么就会消化ACTION_DOWN事件,同时后续的Touch事件会源源不断的传给MyLienar1,与CustomView就没有半毛钱关系了~日志也是这么显示的。

2. 和1一样的条件,如果MyLinear1不愿意处理Touch事件呢?onTouchEvent()返回false,我们看日志:

 11-30 19:28:42.199 E/David--MyLinear(11273): dispatchTouchEvent-Down
11-30 19:28:42.199 E/David--MyLinear(11273): onInterceptTouchEvent-Down
11-30 19:28:42.199 E/David--C---------------(11273): dispatchTouchEvent-Down
11-30 19:28:42.199 E/David--C---------------(11273): onTouchEvent-Down
11-30 19:28:42.199 E/David--C---------------(11273): dispatchTouchEvent() super.dispatchTouchEvent(event) = false
11-30 19:28:42.199 E/David--MyLinear(11273): onTouchEvent-Down
11-30 19:28:42.199 E/David--MyLinear(11273): dispatchTouchEvent() super.dispatchTouchEvent(event) = false

CustomView不愿处理的Touch事件被回传至MyLinear1,这没问题,但是MyLinear1也不愿意处理Touch事件,所以onTouchEvent返回false,同时super.dispatchTouchEvent(event)返回false,后续的Touch事件就不会再传入了~

3. CustomView的dispatchTouchEvent()返回super.dispatchTouchEvent,onTouchEvent返回true,MyLinear1的onTouchEvent返回false,日志如下:

 12-01 12:00:30.735 E/David--MyLinear(  363): dispatchTouchEvent-Down
12-01 12:00:30.735 E/David--MyLinear( 363): onInterceptTouchEvent-Down
12-01 12:00:30.735 E/David--C---------------( 363): dispatchTouchEvent-Down
12-01 12:00:30.735 E/David--C---------------( 363): onTouchEvent-Down
12-01 12:00:30.735 E/David--C---------------( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
12-01 12:00:30.735 E/David--MyLinear( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true 12-01 12:00:30.958 E/David--MyLinear( 363): dispatchTouchEvent-Move
12-01 12:00:30.958 E/David--MyLinear( 363): onInterceptTouchEvent-Move
12-01 12:00:30.958 E/David--C---------------( 363): dispatchTouchEvent-Move
12-01 12:00:30.958 E/David--C---------------( 363): onTouchEvent-Move
12-01 12:00:30.958 E/David--C---------------( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
12-01 12:00:30.958 E/David--MyLinear( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true 12-01 12:00:30.974 E/David--MyLinear( 363): dispatchTouchEvent-Move
12-01 12:00:30.974 E/David--MyLinear( 363): onInterceptTouchEvent-Move
12-01 12:00:30.974 E/David--C---------------( 363): dispatchTouchEvent-Move
12-01 12:00:30.974 E/David--C---------------( 363): onTouchEvent-Move
12-01 12:00:30.974 E/David--C---------------( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
12-01 12:00:30.974 E/David--MyLinear( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true 12-01 12:00:30.992 E/David--MyLinear( 363): dispatchTouchEvent-Move
12-01 12:00:30.992 E/David--MyLinear( 363): onInterceptTouchEvent-Move
12-01 12:00:30.992 E/David--C---------------( 363): dispatchTouchEvent-Move
12-01 12:00:30.992 E/David--C---------------( 363): onTouchEvent-Move
12-01 12:00:30.992 E/David--C---------------( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
12-01 12:00:30.992 E/David--MyLinear( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true 12-01 12:00:31.026 E/David--MyLinear( 363): dispatchTouchEvent-Up
12-01 12:00:31.026 E/David--MyLinear( 363): onInterceptTouchEvent-Up
12-01 12:00:31.026 E/David--C---------------( 363): dispatchTouchEvent-Up
12-01 12:00:31.026 E/David--C---------------( 363): onTouchEvent-Up
12-01 12:00:31.026 E/David--C---------------( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true
12-01 12:00:31.026 E/David--MyLinear( 363): dispatchTouchEvent() super.dispatchTouchEvent(event) = true

CustomView收到Touch事件,而且想处理它,onTouchEvent返回true,此时Touch事件会源源不断地传至CustomView,CustomView和MyLinear1的dispatchTouchEvent都返回true~有两个新特点:

1、MyLinear1的onInterceptTouchEvent每次都会被调用,这说明,如果ViewGroup决定自己处理Touch事件,那么ACTION_DOWN之后的事件都不会经过onInterceptTouchEvent。但是Children View就不一样了,每次都得被拦截一下,谁让你是Children呢~

2、一旦Children View开始处理Touch事件,就没有MyLinear1的onTouchEvent什么事了。

本文总结:

经过上面的分析,应该是说清楚了dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()方法的使用情况了,记住,这三个方法的调用是有顺序的。对于简单的自定义View或者ViewGroup的需求,应该已经能满足需求了。毕竟,最好是不要直接完全重写dispatchTouchEvent或者onTouchEvent,可以适当的加一些判断,但是最终还是要调用View的方法的,这已经能满足绝大多数的需求了。下一篇,咱们仔细研究一下dispatchTouchEvent()和onTouchEvent()方法的代码