本文例子参考:陈育 Android View 事件分发机制源码详解(View篇)
1. activity
public class DispatchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dispatch);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("aaa", "activity onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa", "activity onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa", "activity onTouchEvent up");
break;
}
boolean result = super.onTouchEvent(event);
Log.d("aaa", "activity onTouchEvent result = " + result);
return result;
}
}
2. 布局文件
3. ViewGroup
public class ViewGroupA extends LinearLayout {
public ViewGroupA(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.d("aaa", "ViewGroupA onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa", "ViewGroupA onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa", "ViewGroupA onTouchEvent up");
break;
}
boolean result = super.onTouchEvent(event);
Log.d("aaa", "ViewGroup onTouchEvent result = " + result);
return result;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("aaa", "ViewGroupA dispatchTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa", "ViewGroupA dispatchTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa", "ViewGroupA dispatchTouchEvent up");
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent up");
break;
}
return super.onInterceptTouchEvent(ev);
}
}
4. View
public class ViewA extends View {
public ViewA(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("aaa","ViewA onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa","ViewA onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa","ViewA onTouchEvent up");
break;
}
boolean result = super.onTouchEvent(event);
Log.d("aaa","ViewA onTouchEvent result="+result);
return result;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("aaa","ViewA dispatchTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa","ViewA dispatchTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa","ViewA dispatchTouchEvent up");
break;
}
return super.dispatchTouchEvent(event);
}
}
5. 默认情况
所有事件都用super的方法,日志如下:
ViewGroupA dispatchTouchEvent down
ViewGroupA onInterceptTouchEvent down
ViewA dispatchTouchEvent down
ViewA onTouchEvent down
ViewA onTouchEvent result=false
ViewGroupA onTouchEvent down
ViewGroupA onTouchEvent result = false
activity onTouchEvent down
activity onTouchEvent result = false
activity onTouchEvent move
activity onTouchEvent result = false
activity onTouchEvent move
activity onTouchEvent result = false
activity onTouchEvent up
activity onTouchEvent result = false
可见,ViewGroupA分发事件->拦截事件(默认false)->ViewA分发事件->ViewA的OnTouchEvent方法(super返回false)->ViewGroupA的onTouchEvent(super返回false)->Activity的onTouchEvent。
后续的move和up事件都直接到Activity处。这里的逻辑应该在Activity的dispatchTouchEvent方法中实现。
6. 由于View默认的clickable属性为false,这里改成true
日志如下:
ViewGroupA dispatchTouchEvent down
ViewGroupA onInterceptTouchEvent down
ViewA dispatchTouchEvent down
ViewA onTouchEvent down
ViewA onTouchEvent result=true
ViewGroupA dispatchTouchEvent up
ViewGroupA onInterceptTouchEvent up
ViewA dispatchTouchEvent up
ViewA onTouchEvent up
ViewA onTouchEvent result=true
ViewA中onTouchEvent在Action为down时返回了true,则ViewGroup中是否拦截的第二个条件mFrstTouchTarge就有值,所以up时,还可以继续判断是否拦截。由于事件在ViewA中已经消耗,就不会传到ViewGroup中。
7. ViewGroup拦截ACTION_DOWN事件:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent down");
return true;
case MotionEvent.ACTION_MOVE:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent up");
break;
}
return super.onInterceptTouchEvent(ev);
}
日志如下:
ViewGroupA dispatchTouchEvent down
ViewGroupA onInterceptTouchEvent down
ViewGroupA onTouchEvent down
ViewGroupA onTouchEvent result = false
activity onTouchEvent down
activity onTouchEvent result = false
activity onTouchEvent up
activity onTouchEvent result = false
由于ViewGroupA在down时拦截了事件,则mFrstTouchTarge就没有值,会调用他自己的onTouchEvent,由于他自己的onTouchEvent返回了false,就交给了Activity。尽管Activity的onTouchEvent也返回了false,后序的事件还是会交个他。
8. ViewGroup拦截ACTION_MOVE事件:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent move");
return true;
case MotionEvent.ACTION_UP:
Log.d("aaa", "ViewGroupA onInterceptTouchEvent up");
break;
}
return super.onInterceptTouchEvent(ev);
}
日志如下:
ViewGroupA dispatchTouchEvent down
ViewGroupA onInterceptTouchEvent down
ViewA dispatchTouchEvent down
ViewA onTouchEvent down
ViewA onTouchEvent result=true
ViewGroupA dispatchTouchEvent move
ViewGroupA onInterceptTouchEvent move
ViewA onTouchEvent result=true // *
ViewGroupA dispatchTouchEvent move
ViewGroupA onTouchEvent move
ViewGroupA onTouchEvent result = false
activity onTouchEvent move
activity onTouchEvent result = false
ViewGroupA dispatchTouchEvent up
ViewGroupA onTouchEvent up
ViewGroupA onTouchEvent result = false
activity onTouchEvent up
activity onTouchEvent result = false
除去号那一行,都很好理解。ViewGroupA分发事件,down的时候没有拦截,则到ViewA中消耗掉。接着ViewGroup分发move事件,拦截move事件,处莫名处理。后序分发move事件,由于前一个已经拦截,第二个move直接到ViewGroupA的onTouchEvent处。由于他返回的false,所以最后由Activity处理。
那个*处到底是什么呢?debug进入程序,第一个move时:
if (mFirstTouchTarget == null) {
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {
// 由于down时,ViewA已经消耗掉,这里mFirstTouchTarget有值,不为空,进入这里
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
handled = true;
} else {
// 由于第二次拦截了,所以并没有子View消耗move事件,进入这里
final boolean cancelChild = resetCancelNextUpFlag(target.child)
|| intercepted;
// 下面这行代码非常关键,因为此时cancelChild为true,这个会给子View传一个cancle事件,在子View中把事件cancle掉。
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled = true;
}
if (cancelChild) {
if (predecessor == null) {
// 注意这里,又把mFirstTouchTarget附成了null,所以第二个move不会拦截
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}
}
target的next是target链表中的,这里没有next,所以为null.
// The next target in the target list.
public TouchTarget next;
再看dispatchTransformedTouchEvent方法,如下
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
final boolean handled;
// 保留当前action
final int oldAction = event.getAction();
if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
// 如果为cancle,则进来
event.setAction(MotionEvent.ACTION_CANCEL);
if (child == null) {
handled = super.dispatchTouchEvent(event);
} else {
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
// 这里返回子View的处理结果
return handled;
}
// 这里是不为cancle时的处理
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);
}
}
可见当第一个move到来时,ViewGroupA的事件分发给了子ViewA,并将事件改成了cancle,由于ViewA是clickable为true,所以返回了true。这个事件在ViewA这里消耗掉了,也就没有再到ViewGroupA里。ViewA修改如下:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.d("aaa","ViewA onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.d("aaa","ViewA onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.d("aaa","ViewA onTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("aaa","ViewA onTouchEvent cancle");
break;
}
boolean result = super.onTouchEvent(event);
Log.d("aaa","ViewA onTouchEvent result="+result);
return result;
}
日志如下:
ViewGroupA dispatchTouchEvent down
ViewGroupA onInterceptTouchEvent down
ViewA dispatchTouchEvent down
ViewA onTouchEvent down
ViewA onTouchEvent result=true
ViewGroupA dispatchTouchEvent move
ViewGroupA onInterceptTouchEvent move
ViewA onTouchEvent cancle // * 原*号处
ViewA onTouchEvent result=true // *
ViewGroupA dispatchTouchEvent move
ViewGroupA onTouchEvent move
ViewGroupA onTouchEvent result = false
activity onTouchEvent move
activity onTouchEvent result = false
这也说明了一个View拦截了事件后,后续的所有事件都交由这个View处理,并不会再次判断是否需要拦截。