zoukankan      html  css  js  c++  java
  • Android事件触发机制

    我总结: 这里的返回值有一个"消费(consume)"的概念. 如果放事件(down, move,up)想像为商品就很好理解了. true代表我消费了.(这个商品我要了,你就不能要了,所以就不向后传递). false代表我没消费(这个商品我不要, 我向后传递给你,你爱要不要).
    MotionEvent事件在onInterceptTouchEvent()、onTouchEvent()中的传递顺序

    onInterceptTouchEvent() 用于处理事件并改变事件的传递方向。处理事件这个不用说了,你在函数内部编写代码处理就可以了。而决定传递方向的是返回值,返回为false时事件会传递 给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控 件,这就是所谓的Intercept(截断)。

    onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已 经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你 没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。

    本文源地址:http://www.cnblogs.com/rocky_yi/archive/2011/01/21/1941522.html# ,转载请注明出处!
    <?xml version="1.0" encoding="utf-8"?>
    <com.touchstudy.LayoutView1 xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.touchstudy.LayoutView2
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">
    <com.touchstudy.MyTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv"
    android:text="AB"
    android:textSize="40sp"
    android:textStyle="bold"
    android:background="#FFFFFF"
    android:textColor="#0000FF"/>
    </com.touchstudy.LayoutView2>
    </com.touchstudy.LayoutView1>

    在没有重写onInterceptTouchEvent()和onTouchEvent()的情况下(他们的返回值都是false), 对上面这个布局,MotionEvent事件的传递顺序如下:

    当某个控件的onInterceptTouchEvent()返回值为true时,就会发生截断,事件被传到当前控件的onTouchEvent()。如我们将LayoutView2的onInterceptTouchEvent()返回值为true,则传递流程变成:

     如果我们同时将LayoutView2的onInterceptTouchEvent()和onTouchEvent()设置成true,那么 LayoutView2将消费被传递的事件,同时后续事件(如跟着ACTION_DOWN的ACTION_MOVE或者ACTION_UP)会直接传给 LayoutView2的onTouchEvent(),不传给其他任何控件的任何函数。同时传递给子空间一个ACTION_CANCEL事件。传递流程 变成(图中没有画出ACTION_CANCEL事件):

                            

    示例对应的Java代码(来自http://blog.csdn.net/ddna/archive/2010/04/11/5473293.aspx)
    onInterceptTouchEvent和onTouchEvent调用时序
    分类: Android开发综合 2010-04-11 18:01 14949人阅读 评论(28) 收藏 举报

    onInterceptTouchEvent和onTouchEvent调用时序

    onInterceptTouchEvent()是ViewGroup的一个方法,目的是在系统向该ViewGroup及其各个childView触发onTouchEvent()之前对相关事件进行一次拦截,Android这么设计的想法也很好理解,由于ViewGroup会包含若干childView,因此需要能够统一监控各种touch事件的机会,因此纯粹的不能包含子view的控件是没有这个方法的,如LinearLayout就有,TextView就没有。

    onInterceptTouchEvent()使用也很简单,如果在ViewGroup里覆写了该方法,那么就可以对各种touch事件加以拦截。但是如何拦截,是否所有的touch事件都需要拦截则是比较复杂的,touch事件在onInterceptTouchEvent()和onTouchEvent以及各个childView间的传递机制完全取决于onInterceptTouchEvent()和onTouchEvent()的返回值。并且,针对down事件处理的返回值直接影响到后续move和up事件的接收和传递。

    关于返回值的问题,基本规则很清楚,如果return true,那么表示该方法消费了此次事件,如果return false,那么表示该方法并未处理完全,该事件仍然需要以某种方式传递下去继续等待处理。

    SDK的解释:
    public boolean onInterceptTouchEvent (MotionEvent ev)
    Since: API Level 1

    Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

    Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:
    You will receive the down event here.
    The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
    For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
    If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
    Parametersev The motion event being dispatched down the hierarchy.

    Returns
    Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.



    由于onInterceptTouchEvent()的机制比较复杂,上面的说明写的也比较复杂,总结一下,基本的规则是:

    1. down事件首先会传递到onInterceptTouchEvent()方法

    2. 如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return false,那么后续的move, up等事件将继续会先传递给该ViewGroup,之后才和down事件一样传递给最终的目标view的onTouchEvent()处理。

    3. 如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return true,那么后续的move, up等事件将不再传递给onInterceptTouchEvent(),而是和down事件一样传递给该ViewGroup的onTouchEvent()处理,注意,目标view将接收不到任何事件。

    4. 如果最终需要处理事件的view的onTouchEvent()返回了false,那么该事件将被传递至其上一层次的view的onTouchEvent()处理。

    5. 如果最终需要处理事件的view 的onTouchEvent()返回了true,那么后续事件将可以继续传递给该view的onTouchEvent()处理。

  • 相关阅读:
    指针符号的优先级
    逆序链表建立和输出
    typedef关键字编写步骤
    CasePlayer2-嵌入式软件静态解析工具
    嵌入式软件测试工具和测试方法
    单元测试必要性
    基于winAMS、CasePlayer2嵌入式软件单元测试
    嵌入式软件测试软件--winAMS支持芯片
    基于模型开发 Back-to-Back测试统合工具-MC-Verifier
    单元测试工具-winAMS
  • 原文地址:https://www.cnblogs.com/olvo/p/2447223.html
Copyright © 2011-2022 走看看