Chinaunix首页 | 论坛 | 博客
  • 博客访问: 861021
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: 嵌入式

2011-08-17 15:37:18

onInterceptTouchEvent是ViewGroup特有的,而onTouchEvent是View的,这两个的调用顺序都知道是先onInterceptTouchEvent后onTouchEvent。我这里要说是的onInterceptTouchEvent的拦截过程,先上code:
  1. public class MyLayout extends LinearLayout {
  2.  private static final String TAG = "OnInterceptTouchEventTest";
  3.  public MyLayout(Context context, AttributeSet attrs) {
  4.   super(context, attrs);
  5.  }

  6.  @Override
  7.  public boolean onInterceptTouchEvent(MotionEvent ev) {
  8.   Log.i(TAG, "MyLayout -- onInterceptTouchEvent --- event.getAction() = " + ev.getAction());
  9.   if (ev.getAction() == ev.ACTION_DOWN) {
  10.    return false;
  11.   }
  12.   return true;
  13.  }

  14.  @Override
  15.  public boolean onTouchEvent(MotionEvent event) {
  16.   Log.i(TAG, "MyLayout --- onTouchEvent --- event.getAction() = " + event.getAction());
  17.   return true;
  18.  }
  19. }

  20. public class MyImageView extends ImageView {
  21.  private static final String TAG = "OnInterceptTouchEventTest";
  22.  public MyImageView(Context context, AttributeSet attrs) {
  23.   super(context, attrs);
  24.  }

  25.  @Override
  26.  public boolean onTouchEvent(MotionEvent event) {
  27.   Log.i(TAG, "MyImageView -- event.getAction() = " + event.getAction());
  28.   return true;
  29.  }

06-03 11:13:47.064: INFO/OnInterceptTouchEventTest(21734): MyLayout -- onInterceptTouchEvent --- event.getAction() = 0
06-03 11:13:47.064: INFO/OnInterceptTouchEventTest(21734): MyImageView -- event.getAction() = 0
06-03 11:13:47.142: INFO/OnInterceptTouchEventTest(21734): MyLayout -- onInterceptTouchEvent --- event.getAction() = 2
06-03 11:13:47.142: INFO/OnInterceptTouchEventTest(21734): MyImageView -- event.getAction() = 3
06-03 11:13:47.175: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.192: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.225: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.392: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.425: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.562: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.712: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 2
06-03 11:13:47.842: INFO/OnInterceptTouchEventTest(21734): MyLayout --- onTouchEvent --- event.getAction() = 1

可见,当onInterceptTouchEvent返回true时截获事件,MyLayout的onTouchEvent就被执行,而MyImageView得到cancel事件,onInterceptTouchEvent也不在拦截,直至下一次down事件到来。所以当我们要在ViewGroup中得到非down事件时,down时就不能返回true,而且childView也要返回true,这样onInterceptTouchEvent才会认为自己没处理到,继续截获下一个事件。
阅读(712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~