1.什么是事件分发?
分发的对象:MotionEvent
事件分发:点击事件产生后,如何传递到某个具体的view
2.涉及到的3个方法
public boolean dispatchTouchEvent(MotionEvent ev)
dispatchTouchEvent表示是否消耗当前事件,如果能传递到当前view,则此方法一点会调用。
public boolean onInterceptTouchEvent(MotionEvent ev)
onInterceptTouchEvent表示是否拦截当前事件,若拦截,则在同一个事件序列当中,此方法不会被再次调用。
public boolean onTouchEvent(MotionEvent event)
onTouchEvent表示是否处理当前事件,若不处理,则同一个事件序列当中,当前view无法再次接收到事件。
可以用一段伪代码描述上述3个方法的关系
public boolean dispatchTouchEvent(MotionEvent event){
boolean result = false;
if(onInterceptTouchEvent(event){
result = onTouchEvent(event);
}else{
result = child.dispatchTouchEvent(event);
}
return result;
}
3.几个结论
- 某个view拦截点击事件,则这个事件序列都将由此view处理,并且onInterceptTouchEvent不再调用;
- ViewGroup默认不拦截任何事件,源码默认返回false;
- View无onInterceptTouchEvent拦截方法;
- View的onTouchEvent方法默认是会消耗事件的,除非为不可点击状态。
4.源码分析
- 4.1 activity 分发过程
/**
* Called to process touch screen events. You can override this to
* intercept all touch screen events before they are dispatched to the
* window. Be sure to call this implementation for touch screen events
* that should be handled normally.
*
* @param ev The touch screen event.
*
* @return boolean Return true if this event was consumed.
*/
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
备注:activity无onInterceptTouchEvent方法
过程:
- activity将事件交给所依附的window进行分发,
返回true, 则由某个具体的view进行处理; - 返回false,则activity的onTouchEvent将被调用
- 4.2 ViewGroup 分发过程
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
}
.....
if (!handled && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
}
return handled;
}
ViewGroup的dispatchTouchEvent方法较为复杂,超过200行
大概过程如下:
- 判断当前View(ViewGroup的父类)是否拦截点击事件,若拦截,则onInterceptTouchEvent不会再调用;
- 若不拦截,则遍历所有子元素,并判断是否能否接收到点击事件 ,传到子view中
- 若需要ViewGroup自己处理点击事件(多种原因:拦截了/没有子 view/子 view的onTouchEvent返回false),则会调用super.dispatchTouchEvent(event),即事件交给了view处理
- 4.3 View 分发过程
/**
* Pass the touch screen motion event down to the target view, or this
* view if it is the target.
*
* @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)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
if (!result && onTouchEvent(event)) {
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的处理过程比较简单,因为是单独的view,所以没有拦截方法,只能自己处理,大概过程如下:
- 先判断有无设置onTouchListener,如果设置了且onTouch返回true,则onTouchEvent就不会调用(onTouchListener优先级高于onTouchEvent);
- 条件不符合则进入onTouchEvent
- onTouchEvent默认都是会消耗事件的(返回true),除非为不可点击状态(clickable 和 longClickable都为false, 另外enable属性不影响默认返回值)。