关于ViewGroup中requestDisallowInterceptTouchEvent的用法
2015-03-27 11:26
381 查看
在开发过程中可能会遇到诸如此类问题:
1、在上下滑动的ScrollView中嵌套一个横滑列表,拖动横滑列表时可能引起ScrollView的上下滑动导致体验极差
2、在ViewPager中嵌套了一个横滑列表,在拖动横滑列表时同样可能导致ViewPager的tab切换。
requestDisallowInterceptTouchEvent 是ViewGroup类中的一个公用方法,参数是一个boolean值,官方介绍如下
Called when a child does not want this parent and its ancestors to intercept touch events with
This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.
android系统中,一次点击事件是从父view传递到子view中,每一层的view可以决定是否拦截并处理点击事件或者传递到下一层,如果子view不处理点击事件,则该事件会传递会父view,由父view去决定是否处理该点击事件。在子view可以通过设置此方法去告诉父view不要拦截并处理点击事件,父view应该接受这个请求直到此次点击事件结束。
实际的应用中,可以在子view的ontouch事件中注入父ViewGroup的实例,并调用requestDisallowInterceptTouchEvent去阻止父view拦截点击事件
1、在上下滑动的ScrollView中嵌套一个横滑列表,拖动横滑列表时可能引起ScrollView的上下滑动导致体验极差
2、在ViewPager中嵌套了一个横滑列表,在拖动横滑列表时同样可能导致ViewPager的tab切换。
requestDisallowInterceptTouchEvent 是ViewGroup类中的一个公用方法,参数是一个boolean值,官方介绍如下
Called when a child does not want this parent and its ancestors to intercept touch events with
ViewGroup.onInterceptTouchEvent(MotionEvent).
This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.
android系统中,一次点击事件是从父view传递到子view中,每一层的view可以决定是否拦截并处理点击事件或者传递到下一层,如果子view不处理点击事件,则该事件会传递会父view,由父view去决定是否处理该点击事件。在子view可以通过设置此方法去告诉父view不要拦截并处理点击事件,父view应该接受这个请求直到此次点击事件结束。
实际的应用中,可以在子view的ontouch事件中注入父ViewGroup的实例,并调用requestDisallowInterceptTouchEvent去阻止父view拦截点击事件
public boolean onTouch(View v, MotionEvent event) { ViewGroup viewGroup = (ViewGroup) v.getParent(); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: viewGroup.requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: viewGroup .requestDisallowInterceptTouchEvent(false); break; } }
相关文章推荐
- 关于ViewGroup中requestDisallowInterceptTouchEvent的用法
- Android ViewGroup事件分发机制(requestDisallowInterceptTouchEvent方法)
- View事件分发机制——requestDisallowInterceptTouchEvent的用法
- Android 解决滑动冲突的方法--ViewGroup中的requestDisallowInterceptTouchEvent(true)方法
- requestDisallowInterceptTouchEvent的用法
- 用requestDisallowInterceptTouchEvent()方法防止viewpager和子view滑动事件冲突
- 自定义View有时在使用时设置requestDisallowInterceptTouchEvent(true)无效
- View的requestDisallowInterceptTouchEvent对事件的影响
- Android - requestDisallowInterceptTouchEvent() 阻止父层的View截获touch事件(事件处理机制)
- requestDisallowInterceptTouchEvent解决子View和父View滑动的冲突
- requestDisallowInterceptTouchEvent的用法
- 用requestDisallowInterceptTouchEvent()方法防止viewpager和子view冲突
- getParent().requestDisallowInterceptTouchEvent(true)剥夺父view 对touch 事件的处理权
- android ViewParent requestDisallowInterceptTouchEvent 测试
- 源码分析为什么requestDisallowInterceptTouchEvent(true)能阻止父View拦截事件
- 探究requestDisallowInterceptTouchEvent失效的原因
- android 事件处理机制之 requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- [Android应用]对GroupView中onInterceptTouchEvent方法的理解
- Android事件传递机制以及ViewGroup的onInterceptTouchEvent的理解