標籤:
轉載請註明出處:http://blog.csdn.net/chziroy/article/details/44401615
要理解Android事件分發機制,首先得瞭解幾個概念,也算是總結,假設臨時看不懂也無妨,本文會解說這幾個問題。
1,點擊螢幕,首先事件的傳遞從Activity的dispatchTouchEvent()方法開始。
2,關於Android事件分發機制,相關方法的方法有三個:onTouchEvent(),dispatchTouchEvent(),還有onInterceptTouchEvent(),而相關的類有Activity,View,ViewGroup。
3,時間的分發順序為dispatchTouchEvent --- onInterceptTouchEvent --- onTouchEvent
4,Android事件分發機制,有一個向下分發過程,該過程主要調用dispatchTouchEvent,另一個向上返回過程,主要依靠onTouchEvent方法,
5,Android事件從父視圖分發到子視圖,假設事件被攔截,則事件不會繼續向下分發,而被當前視圖消耗。此時上述的向下分發過程提前結束
6,沒有被消耗的事件,從父視圖逐級分發到子視圖,最後又回到Activity,被Activity中的onTouchEvent()消耗。此時上述的向上返回過程提前結束
本文不會直接貼出上述相關方法和類的源碼,而會貼出其“虛擬碼”,方便理解。
dispatchTouchEvent方法
先從事件分發的起點開始,也就是Activity的dispatchTouchEvent()方法,
public boolean dispatchTouchEvent(MotionEvent ev) { //第一步,將事件分發 //第二步,假設事件在分發中沒被消耗,則傳遞給Activity的onTouchEvent()方法 }
上述代碼的第一步“將事件分發”,那事件會分發到哪裡呢,分發到它的根布局的一個ViewGroup(事實上就算你的activity的布局檔案沒有設定一個LinearLayout這種根布局,系統也會預設給你加一個預設的FrameLayout)。事件分發到了ViewGroup中,就進入了ViewGroup的dispatchTouchEvent 方法,在查看該方法的原始碼時,Android3.0之前該方法的原始碼和更高版本號碼中的原始碼是不同的。只是原理大致同樣。下面是該方法的原理。
public boolean dispatchTouchEvent(MotionEvent ev) { 調用onInterceptTouchEvent檢查是否攔截事件 if(沒有攔截){ 在ViewGroup中遍曆尋找眼下是點擊了哪個子視圖 if(找到了){ 調用該子視圖的dispatchTouchEvent,遞迴下去 }else{ 沒找到,則將事件傳給onTouchListener,沒有Listener則傳給onTouchEvent() 假設再listener或者onTouchEvent()中down事件返回了true,代表事件被消費,興許的move和up都被Listener或者onTouchEvent()處理, 假設down事件返回false,則興許的move,up事件將不會到這一層的Viewgroup,而直接在上一層視圖被消費。 } }else{ 事件被攔截了,原本被點擊的子視圖將接收到一個ACTION_CANCEL事件,而down事件傳給onTouchListener,沒有Listener則傳給onTouchEvent(),依舊遵從上面的down和move,up事件的關係 }}
上述代碼中,關於dispatchTouchEvent 在子視圖中的遞迴調用,假設子視圖是ViewGroup,則依舊進入ViewGroup的dispatchTouchEvent 方法,和上述代碼一樣,假設子視圖是View,則進入View的dispatchTouchEvent ,其代碼和ViewGroup的代碼不大同樣,例如以下
public boolean dispatchTouchEvent(MotionEvent ev) { //假設有listener,則把事件傳遞給listener //假設沒有listener,則把事件傳遞給onTouchEvent()}
onInterceptTouchEvent 方法該方法僅僅在ViewGroup中出現,該原始碼非常easy,並且其凝視非常簡單扼要的描寫敘述了該方法的原理,甚至也描寫敘述了dispatchTouchEvent 的原理,例如以下
/** * Implement this method to intercept all touch screen motion events. This * allows you to watch events as they are dispatched to your children, and * take ownership of the current gesture at any point. * * <p>Using this function takes some care, as it has a fairly complicated * interaction with {@link View#onTouchEvent(MotionEvent) * View.onTouchEvent(MotionEvent)}, and using it requires implementing * that method as well as this one in the correct way. Events will be * received in the following order: * * <ol> * <li> You will receive the down event here. * <li> The down event will be handled either by a child of this view * group, or given to your own onTouchEvent() method to handle; this means * you should implement onTouchEvent() to return true, so you will * continue to see the rest of the gesture (instead of looking for * a parent view to handle it). Also, by returning true from * onTouchEvent(), you will not receive any following * events in onInterceptTouchEvent() and all touch processing must * happen in onTouchEvent() like normal. * <li> For as long as you return false from this function, each following * event (up to and including the final up) will be delivered first here * and then to the target's onTouchEvent(). * <li> If you return true from here, you will not receive any * following events: the target view will receive the same event but * with the action {@link MotionEvent#ACTION_CANCEL}, and all further * events will be delivered to your onTouchEvent() method and no longer * appear here. * </ol> * * @param ev The motion event being dispatched down the hierarchy. * @return Return true to steal motion events from the children and have * them dispatched to this ViewGroup through onTouchEvent(). * The current target will receive an ACTION_CANCEL event, and no further * messages will be delivered here. */ public boolean onInterceptTouchEvent(MotionEvent ev) { return false; }
onTouchEvent方法假設設定了onTouchListener,則不會進入View/ViewGroup的onTouchEvent(),而是進入onTouchListener。假設down事件在某個View/ViewGroup中返回true,則興許的move和up事件將繼續來到當前View/ViewGroup,否則,興許的事件將去到父視圖的onTouchListener或者onTouchEvent,所以假設全部onTouchListener或者onTouchEvent對全部事件都返回false,則終於事件將回到Activity的onTouchEvent,所以事實上Activity中該方法返回什麼都似乎無所謂了。
總結
事實上事件分發機制,能夠分為兩個過程,一個是向下分發過程,一個是向上返回過程,當中向下分發是靠dispatchTouchEvent 方法,從Activity的dispatchTouchEvent 直到目標視圖的dispatchTouchEvent ,然後進入目標視圖的onTouchEvent,向上返回則是靠onTouchEvent,從目標視圖的onTouchEvent直到Activity的onTouchEvent。
而在向下分發的過程可能會提前結束,這受onInterceptTouchEvent影響,也可能是ViewGroup沒有子視圖了,在兩個因素都能夠使該過程提前結束,從而提前進入向上返回的過程。向上返回過程也可能提前結束,假設事件在onTouchEvent中返回true,即事件被消費,那麼事件不會繼續向上傳遞,該過程提前結束。
一言以蔽之:也就是“兩個過程,兩個截斷”
參考過的幾篇比較優秀的部落格
http://codetheory.in/understanding-android-input-touch-events/
http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html#top
https://gist.github.com/Leaking/16e682b1ffac3a59c3df
Android事件分發機制