View的dispatchTouchEvent总结
来源:互联网 发布:ios越狱mac版 编辑:程序博客网 时间:2024/05/18 03:09
View的dispatchTouchEvent派发比较简单,不需要经过打断这一环。
而是直接去调用onTouchEvent方法,注意View如果存在TouchListener,则TouchListener优先。
看源码:
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)) { //noinspection SimplifiableIfStatement ListenerInfo li = mListenerInfo; //如果存在onTouchListener则优先处理,否则去执行onTouchEvent 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; }
再看看onTouchEvent,如果View是CLICKABLE,LONG_CLICKABLE可点击,返回的是true,处理掉
否则 返回false,不处理。
解释一下为什么ListView中的onInterceptTouchEvent只执行了一次。
ListView重写了onInterceptTouchEvent和onTouchEvent,事件分发使用基类ViewGroup的分发,Item的View是ListView的子View,在Item中放一个TextView,发现只在Down的时候onInterceptTouchEvent执行了一次,为什么呢?因为TextView没有CLICKABLE,Down时是不拦截的,而TextView并没有消化事件,所以Down事件被ListView重写的onTouchEvent本身处理了,所以拦截函数就不会执行啦。
如果有给TextView加上onClickListener,则onTouchEvent处理了Down事件,后续的move还是要判断拦截的,到滑动程度就会拦截。
总结:由dispatchTouchEvent派发给TouchListener或onTouchevent
0 0
- View的dispatchTouchEvent总结
- view的dispatchTouchEvent方法
- ViewGroup的dispatchTouchEvent总结
- 关于Android中的View的dispatchTouchEvent
- dispatchTouchEvent总结
- View的onInterceptTouchEvent(MotionEvent ev)和dispatchTouchEvent(MotionEvent ev) 解析
- android dispatchTouchEvent方法查找包含点击坐标的view;
- 13.View的事件分发机制——dispatchTouchEvent详解
- Android事件分发04——View的dispatchTouchEvent
- Android dispatchTouchEvent View事件分发
- 【Android View】Android中View对触摸事件的处理和传递dispatchTouchEvent、onInterceptTouchEvent
- Android事件处理(二)——View的dispatchTouchEvent 函数源码详解
- android事件处理总结--dispatchTouchEvent
- android事件处理总结--dispatchTouchEvent
- android事件处理总结--dispatchTouchEvent
- DispatchTouchEvent的简单运用
- ViewGroup的dispatchTouchEvent理解
- 336_ViewGroup的dispatchTouchEvent
- 开始尝试用C++写算法
- 使用svnkit对svn新增代码统计、代码查询
- 亲自动手制作来自MyBatis-Spring官网的chm格式帮助文件
- 关于TbSchedule任务调度管理框架的整合部署
- 阿里的 com.alibaba.fastjson的日常使用
- View的dispatchTouchEvent总结
- 使用adb shell dumpsys检测Android的Activity任务栈
- 详解Swift中enum类型的用法
- 照片的选择+照片Gridview展示(拖拽排序、删除)+照片viewpager演示(放大、缩小)
- LaTex数学符号公式
- Mybatis中字符串替换问题
- android文件管理器选择apk文件分享时没有蓝牙选项,对方通过蓝牙向本机传输apk文件时失败
- 【Eclipse】Eclipse设置文字编码为UTF-8
- Marathon(4):Port