1. 程式人生 > >觸摸事件的總結(

觸摸事件的總結(

tex line touch 攔截事件 spa 是個 處理 自己的 父類

/**總結:
1.
1.1
dispatchTouchEvent事件分發這個每個都有(無論是View 還是viewgrope,以及Activity)
這個是用來處理是否分發事件(這個總是會被執行的前提是父控件沒有攔截或者進行分發了)
如果這裏就取消事件分發則無法響應本次觸摸(以後的 onInterceptTouchEvent或者 onTouchEvent都不會執行包括父控件的)
1.2
onTouchEvent事件分發這個每個都有(無論是View 還是viewgrope,以及Activity)
用來處理自己的觸摸事件,如果自己返回true則不再給下面的子view分發觸摸事件
1.3
onInterceptTouchEvent這個是是否攔截事件(只有ViewGrope才有)
攔截觸摸事件的,如果dispatchTouchEvent是同意的則可以通過這個來攔截不再給子view分發
2.
如果dispatchTouchEvent為false也就是可以往下給子view分發事件,
但是onInterceptTouchEvent攔截true的時候講不會再給子view了,
包括onTouchEvent處理了事件任意一個為也就是,
3.dispatchTouchEvent true連自己的響應都不會有,也沒有自己的攔截
4.
super.dispatchTouchEvent(ev);並不會和false相等,
如果是flase,子view也不會得到事件分發的處理(如果自己不想攔截處理最好用super,畢竟你不想處理父類還有要處理的東西呢)

以上是個人總結如果有問題或者錯誤希望大家指導,以下是測試部分的響應日誌:

//標準:Activity->LinearLayout->TextView (布局就是LinearLayout裏面一個TextView)
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onInterceptTouchEvent:LinearLayout 攔截
dispatchTouchEvent:TextView 事件分發
onTouchEvent: TextView事件響應
onTouchEvent: LinearLayout事件響應
onTouchEvent: Activity事件響應
dispatchTouchEvent:Activity 事件分發
onTouchEvent: Activity事件響應
*/
/* 攔截後dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onInterceptTouchEvent:LinearLayout 攔截
onTouchEvent: LinearLayout事件響應
onTouchEvent: Activity事件響應
dispatchTouchEvent:Activity 事件分發
onTouchEvent: Activity事件響應*/


/** dispatchTouchEvent true後(不在進行事件分發)
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
*/
/**
* Touch true後
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onInterceptTouchEvent:LinearLayout 攔截
dispatchTouchEvent:TextView 事件分發
onTouchEvent: TextView事件響應
onTouchEvent: LinearLayout事件響應
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onTouchEvent: LinearLayout事件響應
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onTouchEvent: LinearLayout事件響應
*/
/*onInterceptTouchEvent 和Touch為true
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onInterceptTouchEvent:LinearLayout 攔截
onTouchEvent: LinearLayout事件響應
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onTouchEvent: LinearLayout事件響應
dispatchTouchEvent:Activity 事件分發
dispatchTouchEvent:LinearLayout 事件分發
onTouchEvent: LinearLayout事件響應*/



觸摸事件的總結(