重要的点:
onclick没有时,理解为没有消费事件,
源码分析:
E/TAG: onTouchEvent -> 0false经过我的实际测试呢,当view没有onclick事件时,view的ontouchevent()确实是返回false,源码中呢:
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, View child, int desiredPointerIdBits) { final boolean handled; // Canceling motions is a special case. We don't need to perform any transformations // or filtering. The important part is the action, not the contents. final int oldAction = event.getAction(); if (cancel || oldAction == MotionEvent.ACTION_CANCEL) { event.setAction(MotionEvent.ACTION_CANCEL); if (child == null) { handled = super.dispatchTouchEvent(event); } else { handled = child.dispatchTouchEvent(event); } event.setAction(oldAction); return handled; }此时child不为null,所以 handled = child.dispatchTouchEvent(event);为false,所以这个函数返回false,
然后就会执行
if (mFirstTouchTarget == null) { // No touch targets so treat this as an ordinary view. handled = dispatchTransformedTouchEvent(ev, canceled, null, TouchTarget.ALL_POINTER_IDS); } 因为 handled = child.dispatchTouchEvent(event);为false,所以mfirstTouchTarget不会被赋值,为null。
然后就会执行
super.dispatchTouchEvent(event);viewGroup继承自View,也就是说,会执行viewGroup自己的dispatchTouchEvent。ontouch()->ontouchevent()
03-15 10:06:15.103 11165-11165/com.example.chang.test_shijianfenfa D/MyViewGroup: ViewGroup : dispatchTouchEvent: 03-15 10:06:15.103 11165-11165/com.example.chang.test_shijianfenfa D/MyViewGroup: ViewGroup : onInterceptTouchEvent: 03-15 10:06:15.113 11165-11165/com.example.chang.test_shijianfenfa D/MainActivity: View : onTouch: 03-15 10:06:15.113 11165-11165/com.example.chang.test_shijianfenfa E/TAG: onTouchEvent -> 0false 03-15 10:06:15.113 11165-11165/com.example.chang.test_shijianfenfa D/MainActivity: View Group : onTouch: 03-15 10:06:15.113 11165-11165/com.example.chang.test_shijianfenfa D/MyViewGroup: ViewGroup : onTouchEvent: