日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

View的事件分发机制简述

發布時間:2023/12/2 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 View的事件分发机制简述 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要分析的對象就是MotionEvent,點擊事件的事件分發其實就是對MotionEvent事件的分發過程,當MotionEvent產生后,系統需要把這個事件傳遞給一個具體的View,這個傳遞過程就是分發過程。這個過程由三個很重要的方法共同完成:

dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent。?


1、public boolean dispatchTouchEvent(MotionEvent ev) 進行事件分發。Pass the touch screen motion event down to the target view, or this view if it is the target. Parameters ev The motion event to be dispatched. ReturnsTrue if the event was handled by the view, false otherwise.

2、public boolean onInterceptTouchEvent(MotionEvent ev)Added in API level 1 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. Parameters ev The motion event being dispatched down the hierarchy. 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.

在dispatchTouchEvent方法內部調用,用來判斷是否攔截某個事件,若當前View攔截了某個事件,那么在同一事件序列中,此方法不會被再次調用,返回結果表示是否攔截該事件。

3、public boolean onTouchEvent(MotionEvent event)Added in API level 1 Implement this method to handle touch screen motion events. If this method is used to detect click actions, it is recommended that the actions be performed by implementing and callingperformClick(). This will ensure consistent system behavior, including: obeying click sound preferences dispatching OnClickListener calls handling ACTION_CLICK when accessibility features are enabled Parameters event The motion event. True if the event was handled, false otherwise.

View的onTouchEvent方法。在dispatchTouchEvent方法中調用,用來處理點擊事件,返回結果表示是否消耗當前事件,若不消耗,則在同一個事件序列中View無法再次接收到事件。


上述三個方法的關系:

用偽代碼表示:

?

public boolean dispatchTouchEvent(MotionEvent ev){boolean consume = false;if(onInterceptTouchEvent(ev)){consume = onTouchEvent(ev);}else{consume = child.dispatchTouchEvent(ev);}return consume; }

?

由以上偽代碼,點擊事件的傳遞規則:對于一個根ViewGroup來說,點擊事件產生后會首先傳遞給它,這是它的diapatchTouchEvent就會被調用,如果這個ViewGroup的onInterceptTouchEvent方法返回true就表示它要攔截當前事件,接著事件就交給這個ViewGroup處理;若onInterceptTouchEvent方法返回false,就表示它不攔截這個事件,這個事件將繼續傳遞給它的子元素,接著子元素的diapatchTouchEvent方法會被調用,如此反復直到事件被處理。

?

?

總結

以上是生活随笔為你收集整理的View的事件分发机制简述的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。