View事件分发机制——requestDisallowInterceptTouchEvent的用法

来源:互联网 发布:java m5解密 编辑:程序博客网 时间:2024/05/21 22:25

简介

以下是官网介绍

void requestDisallowInterceptTouchEvent (boolean disallowIntercept)
Called when a child does not want this parent and its ancestors to intercept touch events with 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.

 大致意思是:方法requestDisallowInterceptTouchEvent (boolean disallowIntercept)当一个子控件不想让父控件和祖先用方法onInterceptTouchEvent(MontionEvent)拦截自己的触摸事件时调用。
这个父控件应该把这个事件传递给它的父控件(及子控件的祖先)。这个父节点必须在触摸的持续时间内遵守这个请求(也就是说,只有在这个父节点收到一个up或一个cancel之后才清除它)。

使用

先来看下面的应用场景:
在布局文件中定义ScrollView标签,然后在它里面定义一个TextView标签。给TextView设置长按可弹出一个悬浮框,并且悬浮框可在屏幕上移动。当移动悬浮框的时候,悬浮框会消失。这个问题是由于TextView的焦点被ScrollView抢走引起的,用requestDisallowInterceptTouchEvent既可以解决这个问题。可以用下面的代码:
this.getParent().requestDisallowInterceptTouchEvent(true);
含义:当传入的参数为true时,表示子控件要自己消费这次事件,告诉父控件不要拦截(抢走)这次的事件。

原创粉丝点击