摘要:接下来查看方法的源码里面的处理如下如果则返回,即该对触摸事件不予处理。显示了提示框的话则隐藏如果不可点击,则移除长按事件的检测,返回否则继续向下走。当前是点击事件,所以移除长按事件的检测。关于源码中的事件,可以参考文章源码事件
在Android-27中查看源码:
首先我们来查看单个View的触摸事件的处理,在View的dispatchTouchEvent方法中看看源码是如何处理的。
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 gesture stopNestedScroll(); } if (onFilterTouchEventForSecurity(event)) { if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) { result = true; } //noinspection SimplifiableIfStatement ListenerInfo li = mListenerInfo; if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event)) { result = true; } 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; }
dispatchTouchEvent处理过程如下:
如果设置了OnTouchListener和enabled为true,并且onTouch返回为true,即该View在OnTouchListener的onTouch方法中处理了触摸事件。
如果onTouchEvent返回为true,即该View在onTouchEvent中处理了触摸事件。
如果触摸事件在该View中得到处理则返回true,否则返回false(即该View对触摸事件不予处理)。
接下来查看onTouchEvent方法的源码:
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: 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 check removeLongPressCallback(); // Only perform take click actions if we were in the pressed state if (!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)) { performClick(); } } } if (mUnsetPressedState == null) { mUnsetPressedState = new UnsetPressedState(); } if (prepressed) { postDelayed(mUnsetPressedState, ViewConfiguration.getPressedStateDuration()); } else if (!post(mUnsetPressedState)) { // If the post failed, unpress right now mUnsetPressedState.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 away setPressed(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 buttons if (!pointInView(x, y, mTouchSlop)) { // Outside button // Remove any future long press/tap checks removeTapCallback(); removeLongPressCallback(); if ((mPrivateFlags & PFLAG_PRESSED) != 0) { setPressed(false); } mPrivateFlags3 &= ~PFLAG3_FINGER_DOWN; } break; } return true; } return false; }
onTouchEvent里面的处理如下:
如果setEnabled(false),则返回false,即该View对触摸事件不予处理。
如果设置了TouchDelegate,并且TouchDelegate的onTouchEvent返回true,则返回true,即由TouchDelegate中的onTouchEvent方法处理了触摸事件。否则继续向下。
如果该View不可点击并且(viewFlags & TOOLTIP) != TOOLTIP(即当停留或长按在该view时不可显示提示框),则返回false,即该View对触摸事件不予处理。否则继续向下。
根据触摸事件的分类进行处理:
MotionEvent.ACTION_DOWN:
不可点击或者不在可滑动的容器内:如果LONG_CLICKABLE为true则执行OnLongClickListener的onLongClick方法;或者设置了viewFlags为TOOLTIP,则执行showTooltip(显示提示框)。然后返回true,即该View处理了触摸事件,否则继续向下走。
MotionEvent.ACTION_MOVE:在手指移动的过程中判断是否仍在当前view的范围。
MotionEvent.ACTION_UP:
显示了提示框的话则隐藏;
如果不可点击,则移除长按事件的检测,返回true,否则继续向下走。
当前是点击事件,所以移除长按事件的检测。然后执行performClick方法,返回true,即该View处理了触摸事件。
关于ViewGroup源码中的Touch事件,可以参考文章:ViewGroup源码-Touch事件
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/72174.html
摘要:在中查看源码在看完源码的触摸事件源码事件后,我们接着来看看容器类的触摸事件。同样的先来看源码中的方法首先,会判断是否不允许拦截,可以通过进行设置,默认允许拦截。 在Android-27中查看源码: 在看完View源码的触摸事件(View源码-Touch事件)后,我们接着来看看容器类View的触摸事件。因为容器类的View都继承自ViewGroup,所以我们在ViewGroup的源码中来...
摘要:注意,事件分发是向下传递的,也就是父到子的顺序。事件分发机制的本质是要解决,点击事件由哪个对象发出,经过哪些对象,最终达到哪个对象并最终得到处理。表示以及分发给其中在内部完成被赋值。会自己处理事件。 目录介绍 01.Android中事件分发顺序 1.1 事件分发的对象是谁 1.2 事件分发的本质 1.3 事件在哪些对象间进行传递 1.4 事件分发过程涉及方法 1.5 Androi...
摘要:当不拦截事件的时候,事件会向下分发交由它的子或进行处理。表示以及分发给其中在内部完成被赋值。会自己处理事件。 目录介绍 01.Android中事件分发顺序 02.Activity的事件分发机制 2.1 源码分析 2.2 点击事件调用顺序 2.3 得出结论 03.ViewGroup事件的分发机制 3.1 看一下这个案例 3.2 源码分析 3.3 得出结论 04.Vie...
阅读 1311·2021-11-15 11:37
阅读 2567·2021-09-22 10:56
阅读 3394·2021-09-06 15:11
阅读 803·2021-08-31 09:45
阅读 2899·2021-07-28 11:16
阅读 1808·2019-08-30 15:44
阅读 479·2019-08-30 13:22
阅读 3345·2019-08-30 13:18