前言
本文在参考网文的基础上成文,另加自己个人见解。
在自定义控件的过程中,只有掌握了控件的事件分发机制才能更好的掌握自定义控件。
1、同时注册touch和click事件,先执行完touch事件(包括down,move,up)之后再执行click事件
2、对于原生的控件,同时注册两个事件
- Android控件的Listener事件触发顺序是先触发onTouch,其次onClick。
- 如果控件的onTouch返回true将会阻止事件继续传递,返回false事件会继续传递。
3、View和ViewGroup的继承关系图
4、View的dispatchTouchEvent源码(api23)
/**
* 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)) {
//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;
}
再来看一下API 15的View的dispatchTouchEvent源码:
public boolean dispatchTouchEvent(MotionEvent event) {
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onTouchEvent(event, 0);
}
if (onFilterTouchEventForSecurity(event)) {
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
return true;
}
if (onTouchEvent(event)) {
return true;
}
}
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
}
return false;
}
相对来说就简便许多了。API升级的一个重要的原因是出于安全性的考虑。所以API23看起来比API15庞大了许多,大部分源码是出于安全性的考虑。不过其大致的逻辑还是相同的。为了简便起见,先就API15的源码进行一下分析:
事件分发的核心代码:
if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
return true;
}
if (onTouchEvent(event)) {
return true;
}
其中li是View的内部类ListenerInfo的实例,ListenerInfo用于存放一些监听。li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
表明如果设置了touch监听,且控件是ENABLED的就首先执行控件的touch事件即onTouch方法。并且如果该onTouch方法返回true此时停止向下执行,即不执行onTouchEvent方法。由于Click监听的方法onClick是在onTouchEvent方法中执行的,所以如果控件的touch监听返回true,将不执行onClick方法。
&& li.mOnTouchListener.onTouch(this, event)
如果对于mViewFlags &ENABLED_MASK这个位运算不理解,请参考: 对Android源码中常见的一些flag的运算的理解
5、View事件分发原理图
6、onTOuchEvent总结
- onTouchEvent方法中会在ACTION_UP分支中触发onClick的监听。
- 当dispatchTouchEvent在进行事件分发的时候,只有前一个action返回true,才会触发下一个action。