android事件是一级一级传递的,假设父控件不拦截。就传给子控件,假设父控件想要消费事件也就是拦截事件的话,须要重写这种方法

public boolean onInterceptTouchEvent(MotionEvent ev),假设返回true,则父控件自己处理,须要再重写onTouchEvent方法。这时候

@Override

    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (mInputEventConsistencyVerifier != null) {

            mInputEventConsistencyVerifier.onTouchEvent(ev, 1);

        }



        boolean handled = false;

        if (onFilterTouchEventForSecurity(ev)) {

            final int action = ev.getAction();

            final int actionMasked = action & MotionEvent.ACTION_MASK;



            // Handle an initial down.

            if (actionMasked == MotionEvent.ACTION_DOWN) {

                // Throw away all previous state when starting a new touch gesture.

                // The framework may have dropped the up or cancel event for the previous gesture

                // due to an app switch, ANR, or some other state change.

                cancelAndClearTouchTargets(ev);

                resetTouchState();

            }



            // Check for interception.

            final boolean intercepted;

            if (actionMasked == MotionEvent.ACTION_DOWN

                    || mFirstTouchTarget != null) {

                final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;

                if (!disallowIntercept) {

                    intercepted = onInterceptTouchEvent(ev);

                    ev.setAction(action); // restore action in case it was changed

                } else {

                    intercepted = false;

                }

            } else {

                // There are no touch targets and this action is not an initial down

                // so this view group continues to intercept touches.

                intercepted = true;

            }



            // Check for cancelation.

            final boolean canceled = resetCancelNextUpFlag(this)

                    || actionMasked == MotionEvent.ACTION_CANCEL;



            // Update list of touch targets for pointer down, if needed.

            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;

            TouchTarget newTouchTarget = null;

            boolean alreadyDispatchedToNewTouchTarget = false;

            if (!canceled && !intercepted) {

                if (actionMasked == MotionEvent.ACTION_DOWN

                        || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)

                        || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {

                    final int actionIndex = ev.getActionIndex(); // always 0 for down

                    final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)

                            : TouchTarget.ALL_POINTER_IDS;



                    // Clean up earlier touch targets for this pointer id in case they

                    // have become out of sync.

                    removePointersFromTouchTargets(idBitsToAssign);



                    final int childrenCount = mChildrenCount;

                    if (childrenCount != 0) {

                        // Find a child that can receive the event.

                        // Scan children from front to back.

                        final View[] children = mChildren;

                        final float x = ev.getX(actionIndex);

                        final float y = ev.getY(actionIndex);



                        final boolean customOrder = isChildrenDrawingOrderEnabled();

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

                            final int childIndex = customOrder ?

getChildDrawingOrder(childrenCount, i) : i;

                            final View child = children[childIndex];

                            if (!canViewReceivePointerEvents(child)

                                    || !isTransformedTouchPointInView(x, y, child, null)) {

                                continue;

                            }



                            newTouchTarget = getTouchTarget(child);

                            if (newTouchTarget != null) {

                                // Child is already receiving touch within its bounds.

                                // Give it the new pointer in addition to the ones it is handling.

                                newTouchTarget.pointerIdBits |= idBitsToAssign;

                                break;

                            }



                            resetCancelNextUpFlag(child);

                            if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {

                                // Child wants to receive touch within its bounds.

                                mLastTouchDownTime = ev.getDownTime();

                                mLastTouchDownIndex = childIndex;

                                mLastTouchDownX = ev.getX();

                                mLastTouchDownY = ev.getY();

                                newTouchTarget = addTouchTarget(child, idBitsToAssign);

                                alreadyDispatchedToNewTouchTarget = true;

                                break;

                            }

                        }

                    }



                    if (newTouchTarget == null && mFirstTouchTarget != null) {

                        // Did not find a child to receive the event.

                        // Assign the pointer to the least recently added target.

                        newTouchTarget = mFirstTouchTarget;

                        while (newTouchTarget.next != null) {

                            newTouchTarget = newTouchTarget.next;

                        }

                        newTouchTarget.pointerIdBits |= idBitsToAssign;

                    }

                }

            }



            // Dispatch to touch targets.

            if (mFirstTouchTarget == null) {

                // No touch targets so treat this as an ordinary view.

                handled = dispatchTransformedTouchEvent(ev, canceled, null,

                        TouchTarget.ALL_POINTER_IDS);

            } else {

                // Dispatch to touch targets, excluding the new touch target if we already

                // dispatched to it.  Cancel touch targets if necessary.

                TouchTarget predecessor = null;

                TouchTarget target = mFirstTouchTarget;

                while (target != null) {

                    final TouchTarget next = target.next;

                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {

                        handled = true;

                    } else {

                        final boolean cancelChild = resetCancelNextUpFlag(target.child)

                        || intercepted;

                        if (dispatchTransformedTouchEvent(ev, cancelChild,

                                target.child, target.pointerIdBits)) {

                            handled = true;

                        }

                        if (cancelChild) {

                            if (predecessor == null) {

                                mFirstTouchTarget = next;

                            } else {

                                predecessor.next = next;

                            }

                            target.recycle();

                            target = next;

                            continue;

                        }

                    }

                    predecessor = target;

                    target = next;

                }

            }



            // Update list of touch targets for pointer up or cancel, if needed.

            if (canceled

                    || actionMasked == MotionEvent.ACTION_UP

                    || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {

                resetTouchState();

            } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) {

                final int actionIndex = ev.getActionIndex();

                final int idBitsToRemove = 1 << ev.getPointerId(actionIndex);

                removePointersFromTouchTargets(idBitsToRemove);

            }

        }



        if (!handled && mInputEventConsistencyVerifier != null) {

            mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1);

        }

        return handled;

    }



方法里就会推断是否拦截动作

View事件传递,当传给子View时,子View可能会有OnTouch事件或者OnClick事件。这时就要分情况,假设

 public boolean dispatchTouchEvent(MotionEvent event) {

        if (mInputEventConsistencyVerifier != null) {

            mInputEventConsistencyVerifier.onTouchEvent(event, 0);

        }



        if (onFilterTouchEventForSecurity(event)) {

            //noinspection SimplifiableIfStatement

            ListenerInfo li = mListenerInfo;

            if (li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED

                    && li.mOnTouchListener.onTouch(this, event)) {

                return true;

            }



            if (onTouchEvent(event)) {

                return true;

            }

        }



        if (mInputEventConsistencyVerifier != null) {

            mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);

        }

        return false;

    }

这时会先推断是否有设置OnTouchListener,假设有而且是已点击的。就运行onTouch方法,假设返回true则不运行onTouchEvent方法,这时OnClick事件就没有运行了。

仅仅有onTouch返回false才会运行onTouchEvent方法。这里就能够捕捉down跟up、move事件了。在down时要返回true。

最新文章

  1. php伪静态--隐藏地址实际路径方法
  2. dataRow转化为对象
  3. UART的CTS与RTS
  4. JAVA学习中Swing部分JDialog对话框窗体的简单学习
  5. 发送复杂的HTTP GET请求并且取回响应。
  6. 判断A树是否包含B树结构
  7. 客户端把rsyslog重启,就会发送全部日志 --待研究
  8. Ansible2:主机清单【转】
  9. 删除git项目
  10. Python 练习:简单的购物车
  11. pycharm加载多个项目
  12. 利用MATLAB进行曲线拟合
  13. spring boot整合JWT例子
  14. [HTML]去除li前面的小黑点,和ul、LI部分属性
  15. WebLogic 任意文件上传 远程代码执行漏洞 (CVE-2018-2894)-------&gt;&gt;&gt;任意文件上传检测POC
  16. 在线调整InnoDB Buffer Pool Size
  17. 分析公司shareaholic报告:Chrome浏览器使用量居首
  18. java使用链栈实现迷宫求解
  19. xshell5使用ssh连接阿里云服务器
  20. 搭建简单FTP

热门文章

  1. ReportViewer动态加载数据源
  2. sql--关于exec和sp_execute
  3. python 从数据库表生成model
  4. [LeetCode]题解(python):002-Add Two Numbers
  5. Android 百度地图开发问题----解决地图有时候加载不出来问题
  6. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)
  7. 在SQL 中生成JSON数据
  8. eclipse主题插件
  9. C# ikvm 运行htmlunit Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl not found
  10. HTML之框架(frameest、ifram)