zoukankan      html  css  js  c++  java
  • Android事件传递机制【Touch事件】

    作者:lenomon   发布:2011-11-25 13:54   分类:Android

    Android中提供了ViewGroup、View、Activity三个等级的Touch事件处理。也就是说,这三个地方都有事件回调方法。

    测试DEMO视图结构:

     1 <com .orgcent.eventtest.EventLinearLayout
    2 xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:layout_width="fill_parent"
    4 android:layout_height="fill_parent"
    5 android:background="#032d3d"
    6 android:orientation="vertical" >
    7 <com .orgcent.eventtest.EventTextView
    8 android:id="@+id/tvEvent"
    9 android:layout_width="fill_parent"
    10 android:layout_height="100dp"
    11 android:gravity="center"
    12 android:textColor="@android:color/black"
    13 android:background="@android:color/white"
    14 android:text="Activity - ViewGroup - View Event http://orgcent.com dispatch Test"/>
    15 </com>

    Android事件传递机制【Touch事件】

    Android中提供了ViewGroup、View、Activity三个等级的Touch事件处理。也就是说,这三个地方都有事件回调方法。

    测试DEMO视图结构:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <com .orgcent.eventtest.EventLinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:background="#032d3d"
       android:orientation="vertical" >
        <com .orgcent.eventtest.EventTextView
           android:id="@+id/tvEvent"
           android:layout_width="fill_parent"
           android:layout_height="100dp"
           android:gravity="center"
           android:textColor="@android:color/black"
           android:background="@android:color/white"
           android:text="Activity - ViewGroup - View Event http://orgcent.com dispatch Test"/>
    </com>

    至于三者之间的执行顺序,将在下面详细阐述:

    整体上看,事件传递顺序为ViewGroup::onInterceptTouchEvent() –> ViewGroup或View的onTouchEvent() –> Activity::onTouchEvent()

    由于上面每个事件回调方法的返回值不同,导致事件本身及顺序发生微妙变化。下面以返回值为主线来详细阐述:

    需要注意以下两点:
    1、onInterceptTouchEvent()返回true,那么这个方法只会拦截动作ACTION_DOWN。
    2、onInterceptTouchEvent()负责事件分发(事件传递方向),onTouchEvent()负责事件处理(消费)。

    1、ViewGroup的onInterceptTouchEvent()
    返回false:
    默认实现方式。事件(按下、移动、抬起等)将直接传递给目标view(用户触摸的view)。
    在ViewGroup触发,调用ViewGroup::onTouchEvent(),在View触发,调用View::onTouchEvent()。

    androi-touch-event-1
    PS:这里发现ViewGroup::onTouchEvent()也被调用了,原因是View::onTouchEvent()没有处理该事件(返回false),事件将交给父容器处理。

    返回true:
    表示ViewGroup将拦截子View的Touch事件。事件会直接传递到ViewGroup::onTouchEvent()处理。
    也就是说,事件后面的移动、抬起动作不会经过onInterceptTouchEvent(),而是直接传到onTouchEvent()。

    androi-touch-event-2

    2、ViewGroup/View的onTouchEvent()
    返回true:
    表示事件按下动作被处理,意味着事件的移动、抬起等后续动作将会传到此方法。
    如果是View处理,那么ViewGroup的onTouchEvent()将不会获得该事件。

    androi-touch-event-3
    PS:只要onInterceptTouchEvent()返回false,而且目标控件View::onTouchEvent()返回true,那么事件的每一个动作(按下、移动、抬起等)会都会首先传递到onInterceptTouchEvent()中。

    如果是ViewGroup处理,那么Activity不会获得事件。
    androi-touch-event-4

    返回false:
    表示View或ViewGroup不处理事件,系统将把事件传递给其父级处理。

    如果View返回false,那么将由其父容器ViewGroup处理。如果ViewGroup不处理,最终将交给Activity来处理。
    androi-touch-event-5

    如果ViewGroup返回false,将交给最后一级Activity来处理。
    androi-touch-event-6

    3、Activity的onTouchEvent()
    这个方法是事件最后被处理的地方。如果不处理,系统将抛弃这个事件。暂时没有发现这个方法的返回值对程序有什么意义。也许返回true能告诉系统事件被处理了。

    小提示:
    不要直接在程序中调用事件的回调方法。可以使用dispatchTouchEvent(MotionEvent)来分发事件。


    DEMO:android_event_test.zip (56.8 KB)

    本文固定链接: http://orgcent.com/android-touch-event-mechanism/ | 萝卜白菜的博客

  • 相关阅读:
    python 获取当前文件夹下所有文件名
    leetcode 205. Isomorphic Strings
    leetcode 204. Count Primes
    leetcode 203. Remove Linked List Elements
    神经网络中的激活函数tanh sigmoid RELU softplus softmatx
    leetcode 189. Rotate Array
    一个简单的二进制加法器
    AliOS编译安装MyRocks
    MYSQL5.7无法启动服务原因及解决方案
    基础知识巩固笔记(链接、装载与库)
  • 原文地址:https://www.cnblogs.com/armyant/p/2433832.html
Copyright © 2011-2022 走看看