首先所有的View和Activity都有
dispatchTouchEvent//用来分发TouchonTouchEvent//用来响应Touch
而ViewGroup则还有一个
onInterceptTouchEvent//用来拦截Touch
当按下界面时首先是由Activity的 dispatchTouchEvent被调用。然后是根View的dispatchTouchEvent,不断向子View传递,然后由最最后的子View首先响应onTouchEvent,接着父View的onTouchEvent不断被调用。
但是如果当前的View是ViewGroup时,则在dispatchTouchEvent被调用后onInterceptTouchEvent会被调用。
以上的情况都未返回默认的super.xxxTouchEvent的情况。
Activity:如果Activity的dispatchTouchEvent没调用super.dispatchTouchEvent那么Touch事件在此就终结了,后面将不会有任何和Touch有关的方法被调用。
所以说如果重写Activity的dispatchTouchEvent那么一定要调用super.dispatchTouchEvent,除非你不打算让Touch事件向下传递。Activity的dispatchTouchEvent方法的返回值并不会影响Touch事件后续调用。所以返回true,false好像无所谓。
ViewGroup:如果ViewGroup的dispatchTouchEvent没调用super.dispatchTouchEvent并且返回true那么Touch事件在此就终结了,后面将不会有任何和Touch有关的方法被调用。如果返回false那么父View的onTouchEvent将被调用。 如果调用super.dispatchTouchEvent并且返回值为true那么父View的onTouchEvent将不会被调用。View同理。
onInterceptTouchEvent如果返回值为true则表示Touch事件被拦截,子View的dispatchTouchEvent将不会被调用。该方法默认返回false,表示子View的dispatchTouchEvent将会被调用。
onTouchEvent该方法表示触发了Touch事件。如果返回true则表示该事件当前View已经处理完了,父View不用管了。
如果重写了onTouchEvent但又没有调用super.onTouchEvent那么将会影响当前View的OnClickListener等事件的触发。
总结
-
dispatchTouchEvent/onTouchEvent任意一个返回true那么事件在此终结。父View的onTouchEvent将不会被调用。
-
super.dispatchTouchEvent用来调用子View的dispatchTouchEvent和自己的onTouchEvent
-
super.onTouchEvent用来调用自己的OnClickListener等事件。
-
onInterceptTouchEvent的返回值用来表示是否拦截传递给子View的事件。true表示子View的dispatchTouchEvent不被调用。
-
如果当前的View或ViewGroup有OnClickListener等方法那么Touch将在此终结不管dispatchTouchEvent/onTouchEvent的返回值未何。