dispatchTouchEvent方法。终于也是会调用到super.dispatchTouchEvent。我们能够继续往下看Activity的dispatchTouchEvent方法:
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
前面if分支不用管,这里会调用PhoneWindow的superDispatchTouchEvent方法。进去看看:
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
return mDecor.superDispatchTouchEvent(event);
}
调用了DecorView的superDispatchTouchEvent方法,再进去看看:
public boolean superDispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
终于还是调用来DecorView的super.dispatchTouchEvent。也就是说。不管如何。DecorView的dispatchTouchEvent终于都会调用到自己父亲FrameLayout的dispatchTouchEvent方法。而我们在FrameLayout中找不到dispatchTouchEvent方法。所以,会去运行ViewGroup的
dispatchTouchEvent方法。
假设该dispatchTouchEvent返回true,说明后面有view消费掉了该事件,那就返回true,不会再去运行自身的onTouchEvent方法,否则,说明没有view消费掉该事件,会一路回传到Activity中,然后调用自己的onTouchEvent方法。该方法的实现比较简单。例如以下:
public boolean onTouchEvent(MotionEvent event) {
if (mWindow.shouldCloseOnTouch(this, event)) {
finish();
return true;
}
return false;
}
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shouji/article-84987-5.html