android ViewParent requestDisallowInterceptTouchEvent 测试

134 篇文章 0 订阅
22 篇文章 0 订阅

简单测试了一下 requestDisallowInterceptTouchEvent的使用:

两个类:

public static class myView extends View {

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			int action = MotionEventCompat.ACTION_MASK & event.getAction();
			Log.e("FYF", "myView " + SystemUtils.motionEventToString(event));
			if (action == MotionEvent.ACTION_DOWN) {
				getParent().requestDisallowInterceptTouchEvent(true);
			}
			
			return true;
		}
}

public static class myLinearLayout extends LinearLayout {
		@Override
		public boolean onInterceptTouchEvent(MotionEvent ev) {
			// TODO Auto-generated method stub
//			return super.onInterceptTouchEvent(ev);
			int action = MotionEventCompat.ACTION_MASK & ev.getAction();
			
			if (action == MotionEvent.ACTION_DOWN) {
				return false;
			} else {
				return true;
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			Log.e("FYF", "myLinearLayout " + SystemUtils.motionEventToString(event));
			return true;
		}
		
	}

在MyLinearLayout里放一个myView,

MyLineaLayout在interceptTouchEvevnt时,如果action 是 ACTION_DOWN, 那么就return false,将event继续传递给myView,

否则就自己直接截获,


myView没有requestDisallowInterceptTouchEvent(true):

E/FYF     (11126): myView ACTION_DOWN
E/FYF     (11126): myView ACTION_CANCEL
E/FYF     (11126): myLinearLayout ACTION_MOVE
E/FYF     (11126): myLinearLayout ACTION_MOVE
......................................
E/FYF     (11126): myLinearLayout ACTION_UP


myView 运行了requestDisallowInterceptTouchEvent(true):

E/FYF     (12069): myView ACTION_DOWN
E/FYF     (12069): myView ACTION_MOVE
E/FYF     (12069): myView ACTION_MOVE
......................................
E/FYF     (12069): myView ACTION_UP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值