1、 Activity, ViewGroup, View中的触摸事件API
1.1 Activity中的触摸事件API
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
1.2 ViewGroup中的触摸事件API
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
public boolean onInterceptTouchEvent(MotionEvent ev);
1.3 View中的触摸事件API
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
2、dispatchTouchEvent、onInterceptTouchEvent和onTouchEvent的含义
2.1 dispatchTouchEvent:
(1) dispatchTouchEvent和onTouchEvent在Activity、ViewGroup和View中均存在
(2) 它是事件监听的入口,所有事件的触发从它开始。Activity通过dispatchTouchEvent将事件传给ViewGroup,ViewGroup将事件通过dispatchTouchEvent传给下一个ViewGroup或传给View
(3) 它事件机制的核心,onInterceptTouchEvent(), onTouchEvent()以及onTouch()都是通过dispatchTouchEvent进行调度的。
(4) 返回true代表事件被消费,返回false表示事件没有被消费
2.2 onInterceptTouchEvent
(1) 它只存在于ViewGroup中
(2) 如果ViewGroup不想将事件传递给它的子View,则可以在onInterceptTouchEvent中进行拦截。返回true表示ViewGroup拦截该事件,返回false表示ViewGroup没有消费改事件,该事件会继续向下分发给ViewGroup和View
2.3 onTouchEvent
(1) dispatchTouchEvent和onTouchEvent在Activity、ViewGroup和View中均存在
(2)它是对事件的处理。返回true表示它对改事件进行了处理,该事件就不会在向上传递了,返回false表示不处理该事件,事件会继续向外传递。
一张图帮你理解
