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。

Android事件机制全然解析的更多相关文章

  1. Android 进阶学习:事件分发机制全然解析,带你从源代码的角度彻底理解(上)

    http://blog.csdn.net/guolin_blog/article/details/9097463 事实上我一直准备写一篇关于Android事件分发机制的文章,从我的第一篇博客開始,就零 ...

  2. Android事件机制之二:onTouch详解

    <Android事件机制之一:事件传递和消费>一文总结了Android中的事件传递和消费机制. 在其中对OntachEvent中的总结中,不是很具体.本文将主要对onTach进行总结. o ...

  3. Android事件机制之一:事件传递和消费

    http://www.cnblogs.com/lwbqqyumidi/p/3500997.html 关于Android中的事件机制,用到的地方还是很多的,并且这个知识点还真有点复杂. 在写这篇文章前, ...

  4. Android四大组件全然解析(一)---Activity

    本文參考\android\android\frameworks\base\core\java\android\app\Activity.java文件里的类凝视.以及android/frameworks ...

  5. Android事件机制

    一句话描述: 用户和程序之间的互动机制 什么是事件? 用户和程序交互时触发的程序操作. 只要是事件,必须具备三方面: 1 事件的发生者 2 事件接受者 3 事件触发和传递 事件处理的方法 观察者模式: ...

  6. Android IPC机制全解析<一>

    概要 多进程概念及多进程常见注意事项 IPC基础:Android序列化和Binder 跨进程常见的几种通信方式:Bundle通过Intent传递数据,文件共享,ContentProvider,基于Bi ...

  7. Android IPC机制全解析<二>

    在AIDL文件中并不是所有的数据类型都可以使用,AIDL支持的数据类型如下: 基本数据类型(int.long.char.boolean.double等) String和CharSequence Lis ...

  8. Android 事件分发机制具体解释

    很多其它内容请參照我的个人网站: http://stackvoid.com/ 网上非常多关于Android事件分发机制的解释,大多数描写叙述的都不够清晰,没有吧来龙去脉搞清晰,本文将带你从Touch事 ...

  9. 通俗理解Android事件分发与消费机制

    深入:Android Touch事件传递机制全面解析(从WMS到View树) 通俗理解Android事件分发与消费机制 说起Android滑动冲突,是个很常见的场景,比如SliddingMenu与Li ...

随机推荐

  1. HDU OJ 5441 Travel 2015online E

    题目:click here 题意: 有个很暴躁的人,想坐车旅行n个城市.连接城市共有m条路(双向).他坐在车上很不爽,每次最多忍耐x分钟.但是每站下车他又可以休息(重新计时).总共有q次询问.问途中有 ...

  2. 【转】 一个fork的面试题

    转自:一个fork的面试题 前两天有人问了个关于Unix的fork()系统调用的面试题,这个题正好是我大约十年前找工作时某公司问我的一个题,我觉得比较有趣,写篇文章与大家分享一下.这个题是这样的: 题 ...

  3. JSP内置对象----response

    response 对象   当客户访问一个服务器的页面时,会提交一个HTTP 请求,服务器收到请求时,返回HTTP 响应.request 对象获取客户请求提交的信息,  与request对象相对应的对 ...

  4. python自学笔记(十)语句与数据结构应用

    1.最基本的迭代 for x in y 2.如何迭代字典 for x,y in a.items(): print:x,y 3.如何为字典排序 key_list = a.keys() key_list. ...

  5. android LayoutInflater的使用

    看其继承关系: public abstract class LayoutInflater extends Object java.lang.Object ↳ android.view.LayoutIn ...

  6. (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示

    原文 (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows She ...

  7. IPhone多视图切换

    处理IPhone多个view切换是我们常遇到的问题,接下来有个比较实用的方法: 而且还附有创建空项目,内存告急处理和动画效果的实现! 具体步骤: 1.创建一个空的项目,然后添加一个ViewContro ...

  8. spring注解controller示例

    依赖库 spring 3.0 配置web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <w ...

  9. Drying(贪心)

    Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11512   Accepted: 2977 Descripti ...

  10. c基础总结

    机器大小端判断: #include <stdio.h> typedef union{ char x; int i; }un; int main() { un tt; tt.i = ; ) ...