android 事件分發複習

2021-09-27 06:06:06 字數 1540 閱讀 8848

//1.事件在activity 中的傳遞 

public boolean dispatchtouchevent(motionevent ev)

if (getwindow().superdispatchtouchevent(ev))

//如果所有的view 都不消費該事件,那麼事件最終會到達 activity 的ontouchevent

//相反、如果上一步getwindow().superdispatchtouchevent(ev) 返回true 那麼不會到達

//activity 的ontouchevent(ev)

return ontouchevent(ev);

}//注釋說預設實現永遠返回false

public boolean ontouchevent(motionevent event)

return false;

}總結:事件在activity的傳遞,事件從activity的dispatchtouchevent開始,傳遞給getwindow().superdispatchtouchevent(ev),如果返回false 那麼將會傳遞給自己的

ontouchevent(預設實現返回false)

//2.事件在window 中的傳遞 window 是抽象類,實現類phonewindow

//activity 通過getwindow().superdispatchtouchevent(ev),將事件傳遞給了window 物件

//window decor 的關係,activity.setcontentview() 的時候會建立decorview,並根據theme中的//feature 選擇contentparent 布局,setcontentview 最終是新增到contentparent 中id 為//id_android_content中

@override

public boolean superdispatchtouchevent(motionevent event)

//3.事件在viewgroup中傳遞

decorview

public boolean superdispatchtouchevent(motionevent event)

事件在viewgroup 中傳遞的偽**

public boolean dispatchtouchevent(motionevent ev)

// 2.通過for迴圈,遍歷了當前viewgroup下的所有子view

for(int i = 0;i把自己當成乙個view ->view.dispatchtouchevent()

2.不攔截 遍歷子控制項->child.dispatchtouchevent() 如果子控制項返回true 那麼事件傳遞結束

3.如果不攔截、子控制項也不處理->把自己當成乙個view->view.dispatchtouchevent()

}3.view

dispatchtouchevent(motionevent ev)

ontouchevent(ev)

performclick()

Android 事件分發

touch 事件的分發和消費機制dispatchtouchevent onintercepttouchevent 和ontouchevent dispatchtouchevent 事件分發 true 事件會分發給當前view 並由dispatchtouchevent 方法消費,同時停止向下傳 fal...

Android事件分發

android 中與 touch 事件相關的方法包括 dispatchtouchevent motionevent ev onintercepttouchevent motionevent ev ontouchevent motionevent ev 能夠響應這些方法的控制項包括 viewgroup...

Android事件分發

當使用者觸控螢幕時,系統會對觸控事件做出相應的相應,這個事件會產生乙個motionevent,系統根據一定的規則將其傳遞給view進行處理,這個過程就是事件分發機制了。事件的傳遞分為兩個階段,即捕獲階段和冒泡階段。捕獲階段 事件最先由最外層的view接收,然後依次向內層傳遞,直到傳遞到最小的view...