ViewGroupB:
public class ViewGroupB extends ViewGroup{ public ViewGroupB(Context context) { super(context); } public ViewGroupB(Context context, AttributeSet attrs) { super(context, attrs); } public ViewGroupB(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec,heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); for(int i = 0;i < childCount;i++) { View child = getChildAt(i); child.layout(0,0,getMeasuredWidth(),getMeasuredHeight()); } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.e("ViewGroupB","ViewGroupB dispatchTouchEvent" + ev.getAction()); return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { Log.e("ViewGroupB","ViewGroupB onInterceptTouchEvent" + ev.getAction()); return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("ViewGroupB","ViewGroupB onTouchEvent" + event.getAction()); return super.onTouchEvent(event); } }ViewTest:
public class ViewTest extends View{ private Paint paint; public ViewTest(Context context) { super(context); init(); } public ViewTest(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ViewTest(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLACK); paint.setStrokeWidth(10); paint.setStyle(Paint.Style.FILL); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(0,0, Change.dip2px(getContext(),100), Change.dip2px(getContext(),100),paint); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.e("ViewTest","View dispatchTouchEvent" + event.getAction()); return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { Log.e("ViewTest","View onTouchEvent" + event.getAction()); return super.onTouchEvent(event); } }根据我们写入的Log,当我们点击最里面的ViewTest的时候,我们会看到如下的Log输出
08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA dispatchTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA onInterceptTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB dispatchTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB onInterceptTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewTest: View dispatchTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewTest: View onTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewGroupB: ViewGroupB onTouchEvent0 08-30 07:47:13.741 7574-7574/com.byhieg.viewdipatch E/ViewGroupA: ViewGroupA onTouchEvent0这些方法默认返回时false 然后按照如下传递原则: