android事件分发过程中,由viewgroup传递到view

2020-02-18  本文已影响0人  梦里花落知多少_9ce8

具体可以参见ViewGroup中的dispatchTouchEvent方法的源码

if (action == MotionEvent.ACTION_DOWN) {

        if (mMotionTarget != null) {

            mMotionTarget = null;

        }

        if (disallowIntercept || !onInterceptTouchEvent(ev)) {

            ev.setAction(MotionEvent.ACTION_DOWN);

            final int scrolledXInt = (int) scrolledXFloat;

            final int scrolledYInt = (int) scrolledYFloat;

            final View[] children = mChildren;

            final int count = mChildrenCount;

            for (int i = count - 1; i >= 0; i--) {

                final View child = children[i];

                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE

                        || child.getAnimation() != null) {

                    child.getHitRect(frame)

                    //这会遍历viewgroup中所有的子view,看down的点是否在该view区域内。是就分发给该view

                    if (frame.contains(scrolledXInt, scrolledYInt)) {

                        final float xc = scrolledXFloat - child.mLeft;

                        final float yc = scrolledYFloat - child.mTop;

                        ev.setLocation(xc, yc);

                        child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;

                        if (child.dispatchTouchEvent(ev))  {

                            mMotionTarget = child;

                            return true;

                        }

                    }

                }

            }

        }

    }

————————————————

版权声明:本文为CSDN博主「ElisonX」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/elisonx/article/details/94167875

上一篇 下一篇

猜你喜欢

热点阅读