View 事件分发 拦截
2020-10-13 本文已影响0人
米奇小林
贯穿整个事件围绕着 dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()方法。
1.dispatchTouchEvent是处理触摸事件分发,事件(多数情况)是从Activity的dispatchTouchEvent开始的,执行super.dispatchTouchEvent(ev),事件向下分发。(Activity dispatchTouchEvent 方法 实质上是 DecorView 的)
2.onInterceptTouchEvent是ViewGroup提供的方法,默认返回false,返回true表示拦截。
3.onTouchEvent是View中提供的方法,ViewGroup也有这个方法,view中不提供onInterceptTouchEvent。view中默认返回true,表示消费了这个事件。
1.在Activity里,有两个回调函数
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
2.ViewGroup里,有三个回调函数
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onInterceptTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);
3.View里,有两个回调函数
public boolean dispatchTouchEvent(MotionEvent ev);
public boolean onTouchEvent(MotionEvent ev);