简单例子了解View的事件分发(5)

可以看出没有打印出viewTest的onTouchEvent日志,证明确实没有调用,现在设置返回为false, onTouchEvent被调用的日志会出现,受限于篇幅,结果就不放出来了。
接下来,我们在ViewTest的onTouchEvent中设置一个点击监听,查看一些好玩的东西。首先,我先设置我们的ViewTest为可点击,viewTest的onTouch返回值也是false,然后我们看一下onTouchEvent代码:

@Override public boolean onTouchEvent(MotionEvent event) { Log.e("ViewTest","View onTouchEvent" + event.getAction()); Log.e("More","我被调用"); setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Log.e("ViewTest","原来中弹的不是我"); } }); return false; }

这时点击我们的viewTest,我们发现他竟然没有点击事件的日志。这是因为我们直接返回的false,截断了后面的点击事件的触发,所以决定什么都不做,采用默认的实现,即

return super.onTouchEvent(event);

神奇的事情发生了,出现了该有的点击效果。我们查看Log

08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA dispatchTouchEvent0 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA onInterceptTouchEvent0 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB dispatchTouchEvent0 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB onInterceptTouchEvent0 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/ViewTest: View dispatchTouchEvent0 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/onTouch: 我中弹了 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/ViewTest: View onTouchEvent0 08-30 13:07:04.834 16083-16083/com.byhieg.viewdipatch E/More: 我被调用 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA dispatchTouchEvent1 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA onInterceptTouchEvent1 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB dispatchTouchEvent1 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB onInterceptTouchEvent1 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/ViewTest: View dispatchTouchEvent1 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/onTouch: 我中弹了 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/ViewTest: View onTouchEvent1 08-30 13:07:04.931 16083-16083/com.byhieg.viewdipatch E/More: 我被调用 08-30 13:07:04.932 16083-16083/com.byhieg.viewdipatch E/ViewTest: 原来中弹的不是我

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/d0add469b1fd94c063e88aa5f15742b8.html