onInterceptTouchEvent()和onTouchEvent()調用關係

在ViewGroup裏覆寫了onInterceptTouchEvent()方法,就能夠對各類touch事件加以攔截。可是touch事件在onInterceptTouchEvent()和onTouchEvent以及各個childView間的傳遞機制徹底取決於onInterceptTouchEvent()和onTouchEvent()的返回值。java

  1. down事件首先會傳遞到onInterceptTouchEvent()方法。該ViewGroup的onInterceptTouchEvent()在接收到down事件處理完成以後:android

    (1)return false,那麼後續的move, up等事件將繼續會先傳遞給該ViewGroup,以後才和down事件同樣傳遞給最終的目標view的onTouchEvent()處理。spa

    (2)return true,那麼後續的move, up等事件將再也不傳遞給onInterceptTouchEvent(),而是和down事件同樣傳遞給該ViewGroup的onTouchEvent()處理,注意,目標view將接收不到任何事件。 code

  2. 若是最終須要處理事件的view的onTouchEvent()返回了false,那麼該事件將被傳遞至其上一層次的view的onTouchEvent()處理。 orm

  3. 若是最終須要處理事件的view 的onTouchEvent()返回了true,那麼後續事件將能夠繼續傳遞給該view的onTouchEvent()處理。xml


實驗:事件

<?xml version="1.0" encoding="utf-8"?> 
<com.test.LayoutView1 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.test.LayoutView2
        android:orientation="vertical"         
        android:layout_width="fill_parent"         
        android:layout_height="fill_parent"         
        android:gravity="center">        
        <com.test.MyTextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"  
            android:text="AB"/>    
    </com.touchstudy.LayoutView2> 
</com.touchstudy.LayoutView1>
  1. onInterceptTouchEvent()處理down事件均返回false,onTouchEvent處理事件均返回true:
    utf-8

    LayoutView1 : onInterceptTouchEvent  action :  ACTION_DOWNit

    LayoutView2 : onInterceptTouchEvent  action :  ACTION_DOWNio

    MyTextView : onTouchEvent  action :  ACTION_DOWN

    LayoutView1 : onInterceptTouchEvent  action :  ACTION_MOVE

    LayoutView2 : onInterceptTouchEvent  action :  ACTION_MOVE

    MyTextView onTouchEvent  action :  ACTION_MOVE

    這是最多見的狀況,onInterceptTouchEvent並無作任何改變事件傳遞時序的操做,效果上和沒有重寫該方法同樣。各類事件的傳遞自己是自底向上的,次序:LayoutView1 -> LayoutView2 -> MyTextView. 在onInterceptTouchEvent均返回false時,LayoutView1 和 LayoutView2 的onTouchEvent並不會接收到事件,而是最終傳遞給了MyTextView.

  2. LayoutView1 的onInterceptTouchEvent處理down事件返回true,MyTextView的onTouchEvent事件返回true:

    LayoutView1 : onInterceptTouchEvent  action :  ACTION_DOWN

    LayoutView1 : onTouchEvent  action :  ACTION_DOWN

    LayoutView1 : onTouchEvent  action :  ACTION_MOVE

    LayoutView1 : onTouchEvent  action :  ACTION_MOVE

    LayoutView1  : onTouchEvent  action :  ACTION_UP

    LayoutView1在攔截第一次down事件時return true,因此後續事件(包括第一次的down)將由LayoutView1自己處理,事件再也不傳遞下去。

  3. LayoutView1,LayoutView2的onInterceptTouchEvent處理down事件返回false,MyTextView的onTouchEvent事件返回false,LayoutView2的onTouchEvent事件返回true

    LayoutView1 : onInterceptTouchEvent  action :  ACTION_DOWN

    LayoutView2 : onInterceptTouchEvent  action :  ACTION_DOWN

    MyTextView : onTouchEvent  action :  ACTION_DOWN

    LayoutView2 : onTouchEvent  action :  ACTION_DOWN

    LayoutView1 : onInterceptTouchEvent  action :  ACTION_MOVE

    LayoutView2 : onTouchEvent  action :  ACTION_MOVE

    LayoutView1 : onInterceptTouchEvent  action :  ACTION_MOVE

    LayoutView2 : onTouchEvent  action :  ACTION_MOVE

相關文章
相關標籤/搜索