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()方法的代码

Android Touch事件之一:Touch事件在父ViewGroup和子View之间的传递篇的更多相关文章

  1. View事件传递之父View和子View之间的那点事

    Android事件传递流程在网上可以找到很多资料,FrameWork层输入事件和消费事件,可以参考: Touch事件派发过程详解 这篇blog阐述了底层是如何处理屏幕输,并往上传递的.Touch事件传 ...

  2. React中父组件与子组件之间的数据传递和标准化的思考

    React中父组件与子组件之间的数据传递的的实现大家都可以轻易做到,但对比很多人的实现方法,总是会有或多或少的差异.在一个团队中,这种实现的差异体现了每个人各自的理解的不同,但是反过来思考,一个团队用 ...

  3. Iframe父页面与子页面之间的调用

    原文:Iframe父页面与子页面之间的调用 Iframe父页面与子页面之间的调用 专业词语解释如下:     Iframe:iframe元素是文档中的文档.     window对象: 浏览器会在其打 ...

  4. Iframe父页面与子页面之间的相互调用

    iframe元素就是文档中的文档. window对象: 浏览器会在其打开一个HTML文档时创建一个对应的window对象.但是,如果一个文档定义了一个或者多个框架(即:包含一个或者多个frame或者i ...

  5. js父页面和子页面之间传值

    今天和朋友一块讨论,怎样通过js在父页面和子页面之间传值的问题,总结例如以下: 需求描写叙述:父页面有多个子页面.实如今父页面点击子页面,传值到子页面. 看着非常easy,试了好久.主要纠结在怎样获取 ...

  6. Vue2.0如何实现父组件与子组件之间的事件发射与接收

    关于vue2.0的事件发射和接收,大家都知道$dispatch和$broadcast在vue2.0已经被弃用了,取而代之的是更加方便快捷的方式,使用事件中心,组件通过它来互相通信,不管组件在哪一个层都 ...

  7. iframe父窗口和子窗口之间的调用

    1>父窗口获取子窗口 js方法 document.getElementById('if1').contentWindow.document: window.frames["if1&qu ...

  8. vue $emit 父组件与子组件之间的通信(父组件向子组件传参)

    1.首先新建一个子页面为 env.vue的文件(名字这里大家可以自取) 2.然后把子页面引入父页面,代码如图: import env from '@/components/common/env' ex ...

  9. vue父组件与子组件之间的数据传递

    父组件向子组件传递数据 父组件用数据绑定:子组件用props接收 <!-- test-vue-model父组件 --> <template> <div> <m ...

随机推荐

  1. 命令模式(head first 设计模式5)

    一.命令模式定义 命令大家都不会陌生,那么在开始命令模式之前,可以想象一下生活中的命令模式的特点: 如老板命令你完成一个OA项目是一个命令,接着看看其特点: 1.在上面的命令中,命令的执行者肯定是聪明 ...

  2. UVa 11584 划分成回文串

    https://vjudge.net/problem/UVA-11584 题意: 给出一串字符,把它划分成尽量少的回文串. 思路: 用d[i]表示划分到i时所能划分的最小个数,转移方程为d[i]=mi ...

  3. GRASP (职责分配原则)

    要学习设计模式,有些基础知识是我们必须要先知道的,设计模式是关于类和对象的一种高效.灵活的使用方式,也就是说,必须先有类和对象,才能有设计模式的用武之地,否则一切都是空谈,那么类和对象是从那冒出来的呢 ...

  4. 【Docker】性能测试监控平台搭建:InfluxDB+Grafana+Jmeter+cAdvisor

    前言 在做性能测试时,如果有一个性能测试结果实时展示的页面,可以极大的提高我们对系统性能表现的掌握程度,进而提高我们的测试效率.但是我们每次打开Jmeter都会有几个硕大的字提示别用GUI模式进行负载 ...

  5. webpack 多页面支持 & 公共组件单独打包

    webpack - 多页面/入口支持 & 公共组件单独打包 webpack系列目录 webpack 系列 一:模块系统的演进 webpack 系列 二:webpack 介绍&安装 we ...

  6. C#中的?

    1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...

  7. Creating SSL Certificates for CRM Test Environment

    不必找第三方去申请证书了, Windows Server 自己也可以作为一个CA的. When working on a CRM Test environment there are many sce ...

  8. VMware 怎么判断哪台机子试图用混杂模式且不成功

    主要是看个log, 然后推断虚拟端口号 The VMkernel logs at /var/log/vmkernel or /var/log/messages contain entries simi ...

  9. EF学习-获取实体框架01

    实体框架由 EF 设计器(包含在 Visual Studio 中)和 EF Runtime(在 NuGet 上提供)组成. EF 设计器包含在 Visual Studio 中 最新版本的实体框架设计器 ...

  10. 20161212xlVBA文本文件多列合并

    Sub NextSeven_CodeFrame() '应用程序设置 Application.ScreenUpdating = False Application.DisplayAlerts = Fal ...