Android - requestDisallowInterceptTouchEvent() 阻止父层的View截获touch事件(事件处理机制)
来源:互联网 发布:饭店记账软件 编辑:程序博客网 时间:2024/04/20 17:34
研究这个方法之前,首先让我们回顾一下View的onTouchEvent()以及onInterceptTouchEvent()的详细解释
(1)onTouchEvent()
当手指触摸到屏幕时,系统就会调用相应View的onTouchEvent,并传入一系列的action。当有多个层级的View时,在父层级允许的情况下,这个action会一直向下传递直到遇到最深层的View。所以touch事件最先调用的是最底层View的onTouchEent,如果View的onTouchEvent接收到某个touch action并作了相应处理,最后有两种返回方式return true和return false;return true会告诉系统当前的View需要处理这次的touch事件,以后的系统发出的ACTION_MOVE,ACTION_UP还是需要继续监听并接收的,而且这次的action已经被处理掉了,父层的View是不可能触发onTouchEvent了。所以每一个action最多只能有一个onTouchEvent接口返回true。如果return false,便会通知系统,当前View不关心这一次的touch事件,此时这个action会传向父级,调用父级View的onTouchEvent。但是这一次的touch事件之后发出的任何action,该View都不会再接受,onTouchEvent在这一次的touch事件中再也不会触发,也就是说一旦View返回false,那么之后的ACTION_MOVE,ACTION_UP等ACTION就不会在传入这个View,但是下一次touch事件的action还是会传进来的。
(2)onInterceptTouchEvent()
前面说了底层的View能够接收到这次的事件有一个前提条件:在父层级允许的情况下。假设不改变父层级的dispatch方法,在系统调用底层onTouchEvent之前会先调用父View的onInterceptTouchEvent方法判断,父层View是不是要截获本次touch事件之后的action。如果onInterceptTouchEvent返回true,那么本次touch事件之后的所有action都不会再向深层的View传递,统统都会传给本层View的onTouchEvent,就是说父层已经截获了这次touch事件,之后的action也不必询问onInterceptTouchEvent,在这次的touch事件之后发出的action时onInterceptTouchEvent不会再次调用,直到下一次touch事件的来临。如果onInterceptTouchEvent返回false,那么本次action将发送给更深层的View,并且之后的每一次action都会询问父层的onInterceptTouchEvent需不需要截获本次touch事件。只有ViewGroup才有onInterceptTouchEvent方法,因为一个普通的View肯定是位于最深层的View,touch事件能够传到这里已经是最后一站了,肯定会调用View的onTouchEvent。
(3)requestDisallowInterceptTouchEvent()
对于底层的View来说,有一种方法可以阻止父层的View截获touch事件,就是调用getParent().requestDisallowInterceptTouchEvent(true);一旦底层View收到touch的action后调用这个方法那么父层View就不会再调用onInterceptTouchEvent了,也无法截获以后的action
public boolean dispatchTouchEvent(MotionEvent ev) { getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: pager.requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: pager.requestDisallowInterceptTouchEvent(false); break; } }
1 0
- Android - requestDisallowInterceptTouchEvent() 阻止父层的View截获touch事件(事件处理机制)
- getParent().requestDisallowInterceptTouchEvent(true)剥夺父view 对touch 事件的处理权
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制——requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- android 事件处理机制之requestDisallowInterceptTouchEvent
- 文字编辑距离代码实现
- PLMN概念和应用设置
- thinkphp 只在home中应用重定向 路由规则
- RxJava 进击的Rx
- 【Unity】Kinect实现UI控件的点击
- Android - requestDisallowInterceptTouchEvent() 阻止父层的View截获touch事件(事件处理机制)
- Android ListView中复杂数据流的高效渲染
- 手动清除session
- Android Grid View入门知识
- P2P通信标准协议(三)之ICE
- ubuntu安装原生迅雷,让下载成为简单
- Miracast/WIDI浅谈
- 怎样启动、关闭和重新启动oracle监听器 in linux
- *** Error 65: Access violation at 0x00000004 : No 'write' permission