首先进入Activity的dispatchTouchEvent方法。默认会调用userInteraction方法,这里你可以重写此方法处理用户每次点击的需求。其次判断此view下面是否还有可以点击的view。若有,返回true把事件分发给下层view没有调用onTouchEvent方法。
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
当然你在重写dispatchTouchEvent方法时可以不调用父类的dispatchTouchEvent方法,这样导致你不会调用当前view的onTouchEvent方法,也不能往下分发事件。
本文详细解析了Android中Activity的dispatchTouchEvent方法的工作原理。当接收到触摸事件时,该方法首先调用onUserInteraction方法记录用户交互行为,然后检查是否有子View可以处理此事件,并决定是否将事件传递给子View。
6421

被折叠的 条评论
为什么被折叠?



