activity,viewgroup,view
1.預設情況
activity->dispatchtouchevent
mylinearlayout->dispatchtouchevent
mylinearlayout->onintercepttouchevent
customview->dispatchtouchevent
customview->ontouchevent
mylinearlayout->ontouchevent
activity->ontouchevent
activity->dispatchtouchevent
activity->ontouchevent
為什麼dispatchtouchevent會呼叫兩次?
因為有action_down(按下)和action_up(抬起)兩個操作。
2.viewgroup呼叫onintercepttouchevent攔截,返回true
activity->dispatchtouchevent
mylinearlayout->dispatchtouchevent
mylinearlayout->onintercepttouchevent
mylinearlayout->ontouchevent
activity->ontouchevent
activity->dispatchtouchevent
activity->ontouchevent
3.viewgroup呼叫onintercepttouchevent攔截,返回true,並消費ontouchevent,返回true
activity->dispatchtouchevent
mylinearlayout->dispatchtouchevent
mylinearlayout->onintercepttouchevent
mylinearlayout->ontouchevent
activity->dispatchtouchevent
mylinearlayout->dispatchtouchevent
mylinearlayout->ontouchevent
4.在view消費ontouchevent,返回true
activity->dispatchtouchevent
mylinearlayout->dispatchtouchevent
mylinearlayout->onintercepttouchevent
customview->dispatchtouchevent
customview->ontouchevent
activity->dispatchtouchevent
mylinearlayout->dispatchtouchevent
mylinearlayout->onintercepttouchevent
customview->dispatchtouchevent
總結:
a.螢幕按下,先呼叫activity的dispatchevent,然後呼叫viewgroup的dispatchevent,最後呼叫view的dispatchevent
b.dispatchevent在內部會呼叫ontouchevent
c.先呼叫ontouch,再呼叫onclick。ontouch預設返回false,如果返回true的話,ontouchevent不會呼叫
d.在activity中dispatchtouchevent如果不呼叫父類的方法,例如返回false或者true,會攔截所有事件。
f.ontouchevent中父類會呼叫onclick
g.onlongclick的返回值,返回fasle,會繼續傳遞最後呼叫onclick,返回true,最後不會呼叫onclick
View的事件傳遞機制
事件傳遞機制中最重要的兩個函式 dispatchtouchevent motionevent ev 負責分發事件 ontouchevent motionevent event 負責處理事件 activity層 觸控事件發生後首先呼叫activity的dispatchtouchevent motion...
View 事件傳遞
1 所有touch事件都被封裝成了motionevent物件,包括touch的位置 時間 歷史記錄以及第幾個手指 多指觸控 等。2 事件型別分為action down,action up,action move,action pointer down,action pointer up,action...
View 事件傳遞
1 基礎知識 1 所有 touch 事件都被封裝成了 motionevent 物件,包括 touch 的位置 時間 歷史記錄以及第幾個手指 多指觸控 等。2 事件型別分為 action down,action up,action move,action pointer down,action poi...