Android 高级UI 事件传递机制
1.View的事件分发
流程
- dispatchTouchEvent():
- onTouchListener--->onTouch方法
- onTouchEvent
- onClickListener--->onClick方法
ListenerInfo
static class ListenerInfo {
/**
* Listener used to dispatch focus change events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnFocusChangeListener mOnFocusChangeListener;
/**
* Listeners for layout change events.
*/
private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
protected OnScrollChangeListener mOnScrollChangeListener;
/**
* Listeners for attach events.
*/
private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
/**
* Listener used to dispatch click events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
public OnClickListener mOnClickListener;
/**
* Listener used to dispatch long click events.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnLongClickListener mOnLongClickListener;
/**
* Listener used to dispatch context click events. This field should be made private, so it
* is hidden from the SDK.
* {@hide}
*/
protected OnContextClickListener mOnContextClickListener;
/**
* Listener used to build the context menu.
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected OnCreateContextMenuListener mOnCreateContextMenuListener;
private OnKeyListener mOnKeyListener;
private OnTouchListener mOnTouchListener;
private OnHoverListener mOnHoverListener;
private OnGenericMotionListener mOnGenericMotionListener;
private OnDragListener mOnDragListener;
private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
OnApplyWindowInsetsListener mOnApplyWindowInsetsListener;
OnCapturedPointerListener mOnCapturedPointerListener;
}
dispatchTouchEvent
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;
}
onTouchEvent
结论:
- 控件的Listener事件触发的顺序是onTouch,再onClick
- 控件的onTouch返回true,将会使onClick的事件没有了---阻止了事件的传递。返回false,才会传递onClick事件 。
- 如果onTouchListener的onTouch方法返回了true,那么view里面的onTouchEvent就不会被调用了。顺序dispatchTouchEvent-->onTouchListener---return false-->onTouchEvent
- 如果view为disenable,则:onTouchListener里面不会执行,但是会执行onTouchEvent(event)方法
- onTouchEvent方法中的ACTION_UP分支中触发onclick事件监听
onTouchListener-->onTouch方法返回true,消耗次事件。down,但是up事件是无法到达onClickListener.
onTouchListener-->onTouch方法返回false,不会消耗此事件
2.ViewGroup+View的事件分发
ViewGroup继承View
- dispatchTouchEvent()
- onInterceptTouchEvent() (拦截触摸,ViewGroup独有)
- onTouchEvent()
dispatchTouchEvent
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(ev, 1);
}
if (!handled && mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);
}
return handled;
}
onInterceptTouchEvent
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;
}
示例
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
/**
* Created by Xionghu on 2018/6/6.
* Desc:
*/
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
super(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.i("kpioneer", "dispatchTouchEvent:action--"+ev.getAction()+"---view:MyRelativeLayout");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i("kpioneer", "onInterceptTouchEvent:action--"+ev.getAction()+"---view:MyRelativeLayout");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("kpioneer", "onTouchEvent:action--"+event.getAction()+"---view:MyRelativeLayout");
return super.onTouchEvent(event);
}
}
点击Button
06-06 11:05:18.340 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--0---view:MyRelativeLayout
06-06 11:05:18.340 27438-27438/com.haocai.eventdemo I/kpioneer: onInterceptTouchEvent:action--0---view:MyRelativeLayout
06-06 11:05:18.340 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--0
06-06 11:05:18.340 27438-27438/com.haocai.eventdemo I/kpioneer: OnTouchListener:acton--0----view:com.haocai.eventdemo.MyButton{8e32527 VFED..C.. ........ 0,42-264,186 #7f070022 app:id/button1}
06-06 11:05:18.340 27438-27438/com.haocai.eventdemo I/kpioneer: onTouchEvent:action--0
06-06 11:05:18.370 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--2---view:MyRelativeLayout
06-06 11:05:18.370 27438-27438/com.haocai.eventdemo I/kpioneer: onInterceptTouchEvent:action--2---view:MyRelativeLayout
06-06 11:05:18.370 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--2
06-06 11:05:18.370 27438-27438/com.haocai.eventdemo I/kpioneer: OnTouchListener:acton--2----view:com.haocai.eventdemo.MyButton{8e32527 VFED..C.. ...P.... 0,42-264,186 #7f070022 app:id/button1}
06-06 11:05:18.380 27438-27438/com.haocai.eventdemo I/kpioneer: onTouchEvent:action--2
06-06 11:05:18.390 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--2---view:MyRelativeLayout
06-06 11:05:18.390 27438-27438/com.haocai.eventdemo I/kpioneer: onInterceptTouchEvent:action--2---view:MyRelativeLayout
06-06 11:05:18.390 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--2
06-06 11:05:18.390 27438-27438/com.haocai.eventdemo I/kpioneer: OnTouchListener:acton--2----view:com.haocai.eventdemo.MyButton{8e32527 VFED..C.. ...P.... 0,42-264,186 #7f070022 app:id/button1}
06-06 11:05:18.390 27438-27438/com.haocai.eventdemo I/kpioneer: onTouchEvent:action--2
06-06 11:05:18.400 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--2---view:MyRelativeLayout
06-06 11:05:18.400 27438-27438/com.haocai.eventdemo I/kpioneer: onInterceptTouchEvent:action--2---view:MyRelativeLayout
06-06 11:05:18.400 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--2
06-06 11:05:18.400 27438-27438/com.haocai.eventdemo I/kpioneer: OnTouchListener:acton--2----view:com.haocai.eventdemo.MyButton{8e32527 VFED..C.. ...P.... 0,42-264,186 #7f070022 app:id/button1}
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: onTouchEvent:action--2
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--1---view:MyRelativeLayout
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: onInterceptTouchEvent:action--1---view:MyRelativeLayout
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: dispatchTouchEvent:action--1
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: OnTouchListener:acton--1----view:com.haocai.eventdemo.MyButton{8e32527 VFED..C.. ...P.... 0,42-264,186 #7f070022 app:id/button1}
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: onTouchEvent:action--1
06-06 11:05:18.410 27438-27438/com.haocai.eventdemo I/kpioneer: OnClickListener----view:com.haocai.eventdemo.MyButton{8e32527 VFED..C.. ...P.... 0,42-264,186 #7f070022 app:id/button1}
该例子中Button事件点击:
- 先接触到事件的是父容器
- ViewGroup顺序:dispatchTouchEvent--->onInterceptTouchevent-->dispatchTouchEvent(Button)-->OnTouchListener(Button) --->return false---> onTouchEvent(Button)(消耗事件) ----- onTouchevent(该示例父布局并没调用)