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

简介

以下是官网介绍

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时,表示子控件要自己消费这次事件,告诉父控件不要拦截(抢走)这次的事件。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值