一个view中的滑动事件一般在下面的3个方法中实现:(注意:是在view中,不是activity)
dispatchTouchEvent这个方法用来分发TouchEvent
onInterceptTouchEvent这个方法用来拦截TouchEvent
onTouchEvent这个方法用来处理TouchEvent
执行的顺序为:dispatchTouchEvent - onInterceptTouchEvent onTouchEvent
1、dispatchTouchEvent
当TouchEvent发生时,首先Activity将TouchEvent传递给最顶层的View, TouchEvent最先到达最顶层 view 的 dispatchTouchEvent ,然后由 dispatchTouchEvent 方法进行分发,如果dispatchTouchEvent返回true ,则交给这个view的onTouchEvent处理,如果dispatchTouchEvent返回 false ,则交给这个 view 的 interceptTouchEvent 方法来决定是否要拦截这个事件。
2、先说说onInterceptTouchEvent
是定义于ViewGroup里面的一个方法,此事件是用于拦截触摸事件的,ViewGroup(继承自View),一个View的Group,也就是我们的一个布局如LinerLayout,各个布局类都继承自ViewGroup;
作用:如果返回true,表示该事件不往下继续传递,只在当前的view执行,这时候执行当前view的dispatchTouchEventonInterceptTouchEvent onTouchEvent。如果返回false,表示继续往下传递,这时