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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 四 | View 事件传递机制 )

發布時間:2025/6/17 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 四 | View 事件传递机制 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 事件分發 系列文章目錄


【Android 事件分發】事件分發源碼分析 ( 驅動層通過中斷傳遞事件 | WindowManagerService 向 View 層傳遞事件 )
【Android 事件分發】事件分發源碼分析 ( Activity 中各層級的事件傳遞 | Activity -> PhoneWindow -> DecorView -> ViewGroup )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 一 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 二 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 )
【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 四 | View 事件傳遞機制 )


文章目錄

  • Android 事件分發 系列文章目錄
  • 前言
  • 一、View 的事件傳遞機制 ( dispatchTouchEvent )
  • 二、觸摸事件 與 點擊事件 沖突處理
  • 三、View 事件分發相關源碼

前言

接上一篇博客 【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 ) , 繼續分析 ViewGroup 的事件分發機制后續代碼 ;





一、View 的事件傳遞機制 ( dispatchTouchEvent )



在上一篇博客 【Android 事件分發】事件分發源碼分析 ( ViewGroup 事件傳遞機制 三 ) 二、當前遍歷的子組件的事件分發 章節 , 分析到 ViewGroup 的 dispatchTouchEvent 方法中的最終事件分發 , 調用到了 View 的 dispatchTouchEvent 方法繼續向子組件分發觸摸事件 ;

View 組件設置 點擊監聽器 View.OnClickListener , 觸摸監聽器 View.OnTouchListener , 都設置在 View 的 View.ListenerInfo 類型成員中 ;

判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListener , 如果設置了 , 則執行被用戶設置的 觸摸監聽器 OnTouchListener ;
如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true , 此時該分發方法的返回值就是 true ;

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean dispatchTouchEvent(MotionEvent event) {//noinspection SimplifiableIfStatement// 設置的 觸摸監聽器 就是封裝在該對象中 ListenerInfo li = mListenerInfo;// 判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListenerif (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED// 執行被用戶設置的 觸摸監聽器 OnTouchListener&& li.mOnTouchListener.onTouch(this, event)) {// 如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true// 此時該分發方法的返回值就是 true result = true;}} }

如果上述 li.mOnTouchListener.onTouch(this, event) 執行的觸摸監聽器觸摸方法返回值為 true , 則不會調用 View 組件自己的 onTouchEvent 方法了 , 在 onTouchEvent 方法中會調用 點擊監聽器的方法 ;
如果用戶的 觸摸監聽器 OnTouchListener 返回 true , 則 用戶的 點擊監聽器 OnClickListener 會被屏蔽掉 ;
如果同時設置了 點擊監聽器 OnClickListener 和 觸摸監聽器 OnTouchListener , 此時需要做 觸摸事件 與 點擊事件的兼容處理 ;

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean dispatchTouchEvent(MotionEvent event) {// 如果上面為 true ( 觸摸監聽器的觸摸事件處理返回 true ) , 就會阻斷該分支的命中 , 該分支不執行了 // 也就不會調用 View 組件自己的 onTouchEvent 方法 // 因此 , 如果用戶的 觸摸監聽器 OnTouchListener 返回 true // 則 用戶的 點擊監聽器 OnClickListener 會被屏蔽掉 // 如果同時設置了 點擊監聽器 OnClickListener 和 觸摸監聽器 OnTouchListener // 觸摸監聽器 OnTouchListener 返回 false , 點擊監聽器 OnClickListener 才能被調用到 if (!result && onTouchEvent(event)) {result = true;}} }

View 的 onTouchEvent 方法分析 :

Click 點擊事件 , 是一次完整的按下 + 抬起操作 , 如果要判定點擊 , 需要同時有 MotionEvent.ACTION_DOWN + MotionEvent.ACTION_UP 事件 , 因此這里在 MotionEvent.ACTION_UP 事件分支中查找點擊事件 ;

最終找到了點擊事件的調用方法 performClickInternal 方法 ;

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean onTouchEvent(MotionEvent event) {if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {switch (action) {case MotionEvent.ACTION_UP:// 點擊事件 Click 是 按下 + 抬起 事件 // 如果要判定點擊 , 需要同時有 MotionEvent.ACTION_DOWN + MotionEvent.ACTION_UP 事件 if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {// This is a tap, so remove the longpress checkremoveLongPressCallback();// Only perform take click actions if we were in the pressed stateif (!focusTaken) {// Use a Runnable and post this rather than calling// performClick directly. This lets other visual state// of the view update before click actions start.if (mPerformClick == null) {// 此處創建點擊對象 mPerformClick = new PerformClick();}// 調用點擊事件 if (!post(mPerformClick)) {performClickInternal();}}}} }

在 performClickInternal 方法中, 調用了 performClick 方法 ;

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {private boolean performClickInternal() {// Must notify autofill manager before performing the click actions to avoid scenarios where// the app has a click listener that changes the state of views the autofill service might// be interested on.notifyAutofillManagerOnClick();return performClick();} }

在 performClick 方法中 , 調用了 li.mOnClickListener.onClick(this); , li.mOnClickListener 就是用戶設置的點擊事件監聽器 ;

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean performClick() {// We still need to call this method to handle the cases where performClick() was called// externally, instead of through performClickInternal()notifyAutofillManagerOnClick();final boolean result;final ListenerInfo li = mListenerInfo;if (li != null && li.mOnClickListener != null) {playSoundEffect(SoundEffectConstants.CLICK);// 此處直接執行了 點擊監聽器 的點擊方法 li.mOnClickListener.onClick(this);result = true;} else {result = false;}sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);notifyEnterOrExitForAutoFillIfNeeded(true);return result;} }



二、觸摸事件 與 點擊事件 沖突處理



通過分析上述 View 的 dispatchTouchEvent 方法的源碼可知 ,

如果觸摸事件 返回 true ,

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean dispatchTouchEvent(MotionEvent event) {//noinspection SimplifiableIfStatement// 設置的 觸摸監聽器 就是封裝在該對象中 ListenerInfo li = mListenerInfo;// 判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListenerif (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED// 執行被用戶設置的 觸摸監聽器 OnTouchListener&& li.mOnTouchListener.onTouch(this, event)) {// 如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true// 此時該分發方法的返回值就是 true result = true;}} }

則點擊事件就會被屏蔽 ;

public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {public boolean dispatchTouchEvent(MotionEvent event) {// 如果上面為 true ( 觸摸監聽器的觸摸事件處理返回 true ) , 就會阻斷該分支的命中 , 該分支不執行了 // 也就不會調用 View 組件自己的 onTouchEvent 方法 // 因此 , 如果用戶的 觸摸監聽器 OnTouchListener 返回 true // 則 用戶的 點擊監聽器 OnClickListener 會被屏蔽掉 // 如果同時設置了 點擊監聽器 OnClickListener 和 觸摸監聽器 OnTouchListener // 觸摸監聽器 OnTouchListener 返回 false , 點擊監聽器 OnClickListener 才能被調用到 if (!result && onTouchEvent(event)) {result = true;}} }

方法一 : 最簡單的方法是 讓 觸摸事件 返回 false , 這樣 點擊和觸摸 事件 都可以共存 ;

方法二 : 如果一定要讓觸摸事件返回 true , 則只能在觸摸事件中 手動調用 View 的 performClick() 方法 , 但是要注意 控制 觸摸的 按下 , 移動 , 抬起 事件 , 細粒度的分析與控制每個事件的關系 , 然后模擬出點擊事件的調用邏輯 ;





三、View 事件分發相關源碼



public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {/*** Pass the touch screen motion event down to the target view, or this* view if it is the target.** @param event The motion event to be dispatched.* @return True if the event was handled by the view, false otherwise.*/public boolean dispatchTouchEvent(MotionEvent event) {// 無障礙調用 , 輔助功能 // If the event should be handled by accessibility focus first.if (event.isTargetAccessibilityFocus()) {// We don't have focus or no virtual descendant has it, do not handle the event.if (!isAccessibilityFocusedViewOrHost()) {return false;}// We have focus and got the event, then use normal event dispatch.event.setTargetAccessibilityFocus(false);}// 返回結果 boolean result = false;if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onTouchEvent(event, 0);}// 判定手指的動作 final int actionMasked = event.getActionMasked();if (actionMasked == MotionEvent.ACTION_DOWN) {// Defensive cleanup for new gesturestopNestedScroll();}if (onFilterTouchEventForSecurity(event)) {if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {result = true;}//noinspection SimplifiableIfStatement// 設置的 觸摸監聽器 就是封裝在該對象中 ListenerInfo li = mListenerInfo;// 判斷該組件是否被用戶設置了 觸摸監聽器 OnTouchListenerif (li != null && li.mOnTouchListener != null&& (mViewFlags & ENABLED_MASK) == ENABLED// 執行被用戶設置的 觸摸監聽器 OnTouchListener&& li.mOnTouchListener.onTouch(this, event)) {// 如果用戶設置的 觸摸監聽器 OnTouchListener 觸摸方法返回 true// 此時該分發方法的返回值就是 true result = true;}// 如果上面為 true ( 觸摸監聽器的觸摸事件處理返回 true ) , 就會阻斷該分支的命中 , 該分支不執行了 // 也就不會調用 View 組件自己的 onTouchEvent 方法 // 因此 , 如果用戶的 觸摸監聽器 OnTouchListener 返回 true // 則 用戶的 點擊監聽器 OnClickListener 會被屏蔽掉 // 如果同時設置了 點擊監聽器 OnClickListener 和 觸摸監聽器 OnTouchListener // 觸摸監聽器 OnTouchListener 返回 false , 點擊監聽器 OnClickListener 才能被調用到 if (!result && onTouchEvent(event)) {result = true;}}if (!result && mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);}// Clean up after nested scrolls if this is the end of a gesture;// also cancel it if we tried an ACTION_DOWN but we didn't want the rest// of the gesture.if (actionMasked == MotionEvent.ACTION_UP ||actionMasked == MotionEvent.ACTION_CANCEL ||(actionMasked == MotionEvent.ACTION_DOWN && !result)) {stopNestedScroll();}return result;}/*** Implement this method to handle touch screen motion events.* <p>* If this method is used to detect click actions, it is recommended that* the actions be performed by implementing and calling* {@link #performClick()}. This will ensure consistent system behavior,* including:* <ul>* <li>obeying click sound preferences* <li>dispatching OnClickListener calls* <li>handling {@link AccessibilityNodeInfo#ACTION_CLICK ACTION_CLICK} when* accessibility features are enabled* </ul>** @param event The motion event.* @return True if the event was handled, false otherwise.*/public boolean onTouchEvent(MotionEvent event) {final float x = event.getX();final float y = event.getY();final int viewFlags = mViewFlags;final int action = event.getAction();final boolean clickable = ((viewFlags & CLICKABLE) == CLICKABLE|| (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)|| (viewFlags & CONTEXT_CLICKABLE) == CONTEXT_CLICKABLE;if ((viewFlags & ENABLED_MASK) == DISABLED) {if (action == MotionEvent.ACTION_UP && (mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;// A disabled view that is clickable still consumes the touch// events, it just doesn't respond to them.return clickable;}if (mTouchDelegate != null) {if (mTouchDelegate.onTouchEvent(event)) {return true;}}if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {switch (action) {case MotionEvent.ACTION_UP:// 點擊事件 Click 是 按下 + 抬起 事件 // 如果要判定點擊 , 需要同時有 MotionEvent.ACTION_DOWN + MotionEvent.ACTION_UP 事件 mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;if ((viewFlags & TOOLTIP) == TOOLTIP) {handleTooltipUp();}if (!clickable) {removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;break;}boolean prepressed = (mPrivateFlags & PFLAG_PREPRESSED) != 0;if ((mPrivateFlags & PFLAG_PRESSED) != 0 || prepressed) {// take focus if we don't have it already and we should in// touch mode.boolean focusTaken = false;if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {focusTaken = requestFocus();}if (prepressed) {// The button is being released before we actually// showed it as pressed. Make it show the pressed// state now (before scheduling the click) to ensure// the user sees it.setPressed(true, x, y);}if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {// This is a tap, so remove the longpress checkremoveLongPressCallback();// Only perform take click actions if we were in the pressed stateif (!focusTaken) {// Use a Runnable and post this rather than calling// performClick directly. This lets other visual state// of the view update before click actions start.if (mPerformClick == null) {// 此處創建點擊對象 mPerformClick = new PerformClick();}// 調用點擊事件 if (!post(mPerformClick)) {performClickInternal();}}}if (mUnsetPressedState == null) {mUnsetPressedState = new UnsetPressedState();}if (prepressed) {postDelayed(mUnsetPressedState,ViewConfiguration.getPressedStateDuration());} else if (!post(mUnsetPressedState)) {// If the post failed, unpress right nowmUnsetPressedState.run();}removeTapCallback();}mIgnoreNextUpEvent = false;break;case MotionEvent.ACTION_DOWN:if (event.getSource() == InputDevice.SOURCE_TOUCHSCREEN) {mPrivateFlags3 |= PFLAG3_FINGER_DOWN;}mHasPerformedLongPress = false;if (!clickable) {checkForLongClick(0, x, y);break;}if (performButtonActionOnTouchDown(event)) {break;}// Walk up the hierarchy to determine if we're inside a scrolling container.boolean isInScrollingContainer = isInScrollingContainer();// For views inside a scrolling container, delay the pressed feedback for// a short period in case this is a scroll.if (isInScrollingContainer) {mPrivateFlags |= PFLAG_PREPRESSED;if (mPendingCheckForTap == null) {mPendingCheckForTap = new CheckForTap();}mPendingCheckForTap.x = event.getX();mPendingCheckForTap.y = event.getY();postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());} else {// Not inside a scrolling container, so show the feedback right awaysetPressed(true, x, y);checkForLongClick(0, x, y);}break;case MotionEvent.ACTION_CANCEL:if (clickable) {setPressed(false);}removeTapCallback();removeLongPressCallback();mInContextButtonPress = false;mHasPerformedLongPress = false;mIgnoreNextUpEvent = false;mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;break;case MotionEvent.ACTION_MOVE:if (clickable) {drawableHotspotChanged(x, y);}// Be lenient about moving outside of buttonsif (!pointInView(x, y, mTouchSlop)) {// Outside button// Remove any future long press/tap checksremoveTapCallback();removeLongPressCallback();if ((mPrivateFlags & PFLAG_PRESSED) != 0) {setPressed(false);}mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN;}break;}return true;}return false;}/*** Entry point for {@link #performClick()} - other methods on View should call it instead of* {@code performClick()} directly to make sure the autofill manager is notified when* necessary (as subclasses could extend {@code performClick()} without calling the parent's* method).*/private boolean performClickInternal() {// Must notify autofill manager before performing the click actions to avoid scenarios where// the app has a click listener that changes the state of views the autofill service might// be interested on.notifyAutofillManagerOnClick();return performClick();}/*** Call this view's OnClickListener, if it is defined. Performs all normal* actions associated with clicking: reporting accessibility event, playing* a sound, etc.** @return True there was an assigned OnClickListener that was called, false* otherwise is returned.*/// NOTE: other methods on View should not call this method directly, but performClickInternal()// instead, to guarantee that the autofill manager is notified when necessary (as subclasses// could extend this method without calling super.performClick()).public boolean performClick() {// We still need to call this method to handle the cases where performClick() was called// externally, instead of through performClickInternal()notifyAutofillManagerOnClick();final boolean result;final ListenerInfo li = mListenerInfo;if (li != null && li.mOnClickListener != null) {playSoundEffect(SoundEffectConstants.CLICK);// 此處直接執行了 點擊監聽器 的點擊方法 li.mOnClickListener.onClick(this);result = true;} else {result = false;}sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);notifyEnterOrExitForAutoFillIfNeeded(true);return result;}}

源碼路徑 : /frameworks/base/core/java/android/view/View.java

總結

以上是生活随笔為你收集整理的【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 四 | View 事件传递机制 )的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 精品播放 | 欧美日韩精品一区二区三区蜜桃 | 高清精品xnxxcom | 久草免费在线视频观看 | 在线观看国产亚洲 | 天堂在线观看中文字幕 | 国产精品久久网 | 30一40一50女人毛片 | 天天操夜夜摸 | 亚洲激情一区二区三区 | 亚洲伦理在线播放 | 在线观看日韩视频 | 国产精品久免费的黄网站 | 99热| 在线观看福利电影 | 四虎影库永久在线 | 国产一区二区麻豆 | 国产精品一区二区av白丝下载 | 亚洲欧美日韩成人 | 色视频在线免费观看 | 中文字幕+乱码+中文乱码www | 美女极度色诱图片www视频 | 9999久久久久 | 午夜少妇av | 免费日韩毛片 | 欧美在线观看www | 超碰激情| cekc老妇女cea0 | 热99在线 | 久久金品 | 加勒比精品| 亚洲精品无amm毛片 国内一区二区三区 | 激情五月五月婷婷 | 96超碰在线 | 亚洲天堂第一 | 四虎影视在线播放 | 精品少妇一二三区 | 亚洲精品aaa | 日本一区二区色 | 成人影视免费观看 | 天天综合色 | 久久中文字幕在线观看 | 欧美在线观看免费高清 | 狠狠干狠狠干狠狠干 | 欧美日韩一区二区不卡 | 三级全黄视频 | 91视频免费观看网站 | 中文婷婷 | 亚洲欧美成人一区二区三区 | 人妻丰满熟妇av无码久久洗澡 | 污污视频免费网站 | 国精产品乱码一区一区三区四区 | 国产a级淫片| 亚洲中文字幕无码av永久 | 理论片高清免费理伦片 | av资源共享 | 欧美黄色免费视频 | 成人国产一区二区 | 人人舔人人爽 | 麻豆三级视频 | 天天视频入口 | 久草视频免费在线 | 成年人拍拍视频 | 免费日本视频 | 青青草久久伊人 | 啪啪啪毛片 | 久久久久99精品成人片 | 清纯唯美亚洲 | 污污视频网站免费观看 | 中文天堂在线资源 | 欧美日本色图 | 91九色蝌蚪 | av在线免播放器 | 97天天操| 蜜色影院 | 老熟妇一区二区三区啪啪 | 久久免费小视频 | 香蕉视频传媒 | 韩国成人在线 | 中文字幕国产专区 | 涩涩视频网站在线观看 | 中国黄色一级毛片 | 午夜激情电影在线观看 | www.夜色| 97精品人妻一区二区 | 五十路六十路 | 秋霞影院午夜丰满少妇在线视频 | 国产欧美一区二区三区在线看蜜臀 | 亚洲乱码在线观看 | 色哟哟官网 | 韩国久久久久 | www.猫咪av | 日本精品一区二区三区四区的功能 | 日本捏奶吃奶的视频 | 天天看夜夜 | 国产成人精品无码免费看在线 | 夜夜操夜夜爽 | 免费日皮视频 | av噜噜|