Android 進階學習:Android事件分發onInterceptTouchEvent與onTouchEvent

來源:互聯網
上載者:User

http://blog.csdn.net/coder80/article/details/8333904

最近在做一個項目,需要定製一個View,類似於Launcher中水平滾動效果。當初仿照Workspace類來實現。然而在針對一個子View響應Scroll事件時,這個子View死活都不響應觸摸事件。分析了一天,最後給這個子View的屬性中添加android:clickable="true", 問題就被搞定了。       Android平台事件分發的兩個函數onInterceptTouchEvent與onTouchEvent,還是比較複雜的。關鍵點還是在於其傳回值。現將網上的一些文章借鑒,使用一個簡單的demo,進行一些分析。onInterceptTouchEvent()是ViewGroup的一個方法,目的是在系統向該ViewGroup及其各個childView觸發onTouchEvent()之前對相關事件進行一次攔截.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()處理。並且,childView將接收不到任何事件。4.如果最終需要處理事件的view的onTouchEvent()返回了false,表示事件沒有被消耗,那麼該事件將被傳遞至其上一層次的view的onTouchEvent()處理。5.如果最終需要處理事件的view 的onTouchEvent()返回了true,那麼後續事件將可以繼續傳遞給該view的onTouchEvent()處理。    通過一個簡單的demo,對上述幾條資訊進行逐一講解。三個類自訂類分別繼承於LinearLayout,ALayout BLayout CLayout(僅僅貼出ALayout類的代碼,其他類的代碼類似)public class ALayout extends LinearLayout{public ALayout(Context context) {super(context);// TODO Auto-generated constructor stub}public ALayout(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}
public ALayout(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}
@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubfinal int action = ev.getAction();switch (action & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:Log.e("ALayout","onInterceptTouchEvent ACTION_DOWN called,return = " + super.onInterceptTouchEvent(ev));break;case MotionEvent.ACTION_MOVE:break;case MotionEvent.ACTION_UP:break;}return super.onInterceptTouchEvent(ev);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubfinal int action = event.getAction();switch (action & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:Log.e("ALayout","onTouchEvent ACTION_DOWN called,return = " + super.onTouchEvent(event));break;case MotionEvent.ACTION_MOVE:break;case MotionEvent.ACTION_UP:break;}return super.onTouchEvent(event);}}
布局檔案main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <com.example.touchtest.ALayout
        android:id="@+id/view_a"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff0000"
        android:gravity="center" >
        <com.example.touchtest.BLayout
            android:id="@+id/view_b"
            android:layout_width="250dip"
            android:layout_height="250dip"
            android:background="#ff00ff00"
            android:gravity="center" >
            <com.example.touchtest.CLayout
                android:id="@+id/view_c"
                android:layout_width="150dip"
                android:layout_height="150dip"
                android:background="#ff0000ff"
                android:gravity="center" >
            </com.example.touchtest.CLayout>
        </com.example.touchtest.BLayout>
    </com.example.touchtest.ALayout>
</RelativeLayout>
程式運行後效果如下所示:

    

應用運行效果如左圖所示。紅色背景為ALayout,綠色為BLayout,藍色為CLayout.點擊藍色(CLayout)地區時,查看log如下:

E/ALayout(10407): onInterceptTouchEvent ACTION_DOWN called,return = false

E/BLayout(10407): onInterceptTouchEvent ACTION_DOWN called,return = false

E/CLayout(10407): onInterceptTouchEvent ACTION_DOWN called,return = false

E/CLayout(10407): onTouchEvent ACTION_DOWN called,return = false

E/BLayout(10407): onTouchEvent ACTION_DOWN called,return = false

E/ALayout(10407): onTouchEvent ACTION_DOWN called,return = false

通過log得知,ACTION_DOWN事件最開始是由onInterceptTouchEvent函數進行處理。之後傳遞給onTouchEvent函數。這兩個函數預設的傳回值為false。由於A,B和C三個View均沒有消耗掉這個ACTION_DOWN事件,則這個事件也就不了了之了。現在修改一點內容,將BLayout的屬性中添加一條android:clickable="true",即布局檔案為:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.touchtest.ALayout        android:id="@+id/view_a"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#ffff0000"        android:gravity="center" >        <com.example.touchtest.BLayout            android:id="@+id/view_b"            android:layout_width="250dip"            android:layout_height="250dip"
            android:clickable="true"
            android:background="#ff00ff00"            android:gravity="center" >            <com.example.touchtest.CLayout                android:id="@+id/view_c"                android:layout_width="150dip"                android:layout_height="150dip"                android:background="#ff0000ff"                android:gravity="center" >            </com.example.touchtest.CLayout>        </com.example.touchtest.BLayout>    </com.example.touchtest.ALayout></RelativeLayout>運行後,點擊藍色地區,顯示的log資訊為:
E/ALayout(10764): onInterceptTouchEvent ACTION_DOWN called,return = false
E/BLayout(10764): onInterceptTouchEvent ACTION_DOWN called,return = false
E/CLayout(10764): onInterceptTouchEvent ACTION_DOWN called,return = false
E/CLayout(10764): onTouchEvent ACTION_DOWN called,return = false
E/BLayout(10764): onTouchEvent ACTION_DOWN called,return = true
由於只有BLayout響應click事件,onTouchEvent函數捕獲到DOWN事件後,消耗掉此事件。接下來的MOVE和UP事件均傳遞給BLayout了。由此可以得出結論: onTouchEvent 中,傳回值是 true ,則說明消耗掉了這個事件,傳回值是 false ,則沒有消耗掉,會繼續傳遞下去。      接下來,做第三個測試。在CLayout上,添加一個Button。布局檔案如下所示:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.touchtest.ALayout        android:id="@+id/view_a"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#ffff0000"        android:gravity="center" >        <com.example.touchtest.BLayout            android:id="@+id/view_b"            android:layout_width="250dip"            android:layout_height="250dip"            android:clickable="true"            android:background="#ff00ff00"            android:gravity="center" >            <com.example.touchtest.CLayout                android:id="@+id/view_c"                android:layout_width="150dip"                android:layout_height="150dip"                android:clickable="false"                android:background="#ff0000ff"                android:gravity="center" >                <Button                    android:id="@+id/button1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="Button" />            </com.example.touchtest.CLayout>        </com.example.touchtest.BLayout>    </com.example.touchtest.ALayout></RelativeLayout>響應的Activity中添加Button響應事件處理        mBtn = (Button) findViewById(R.id.button1);        mBtn.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubLog.e("","mBtn was click...");}});運行後,效果


  點擊Button後,log資訊如下 

E/ALayout(10856):onInterceptTouchEvent ACTION_DOWN called,return = false

E/BLayout(10856): onInterceptTouchEvent ACTION_DOWN called,return = false

E/CLayout(10856): onInterceptTouchEvent ACTION_DOWN called,return = false

E/(10856): mBtn was click...

從log分析得知,Button處於最上層的一個View,有Btn截獲點擊事件,不在傳遞給B,C,A。

第三個測試,說明了"江湖流言"4,5兩條

4.如果最終需要處理事件的view的onTouchEvent()返回了false,表示事件沒有被消耗,那麼該事件將被傳遞至其上一層次的view的onTouchEvent()處理。5.如果最終需要處理事件的view 的onTouchEvent()返回了true,那麼後續事件將可以繼續傳遞給該view的onTouchEvent()處理。 接著做第四個測試。依舊使用上面的代碼,但要在ALayout的View中添加一條屬性。android:clickable="true"。並且ALayout中的onInterceptTouchEvent函數傳回值為true。其修改後的代碼如下所示:ALayout.Java檔案@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubfinal int action = ev.getAction();switch (action & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN:Log.e("ALayout","onInterceptTouchEvent ACTION_DOWN called,return = true");break;case MotionEvent.ACTION_MOVE:break;case MotionEvent.ACTION_UP:break;}// return super.onInterceptTouchEvent(ev);return true;}布局檔案為:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.touchtest.ALayout        android:id="@+id/view_a"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#ffff0000"        android:clickable="true"        android:gravity="center" >        <com.example.touchtest.BLayout            android:id="@+id/view_b"            android:layout_width="250dip"            android:layout_height="250dip"            android:clickable="true"            android:background="#ff00ff00"            android:gravity="center" >            <com.example.touchtest.CLayout                android:id="@+id/view_c"                android:layout_width="150dip"                android:layout_height="150dip"                android:clickable="true"                android:background="#ff0000ff"                android:gravity="center" >                <Button                    android:id="@+id/button1"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:text="Button" />            </com.example.touchtest.CLayout>        </com.example.touchtest.BLayout>    </com.example.touchtest.ALayout></RelativeLayout>運行後,點擊Button,log資訊如下所示:12-19 22:49:34.722: E/ALayout(11032): onInterceptTouchEvent ACTION_DOWN called,return = true12-19 22:49:34.722: E/ALayout(11032): onTouchEvent ACTION_DOWN called,return = true由第四個測試印證了"江湖流言"2,3兩條。2.如果該ViewGroup的onInterceptTouchEvent()在接收到down事件處理完成之後return false,那麼後續的move,   up等事件將繼續會先傳遞給該ViewGroup,之後才和down事件一樣傳遞給最終的目標view的onTouchEvent()處理。3.如果該ViewGroup的onInterceptTouchEvent()在接收到down事件處理完成之後return true,那麼後續的move, up等事件將不再傳遞給onInterceptTouchEvent(),而是和down事件一樣傳遞給該ViewGroup的onTouchEvent()處理。並且,childView將接收不到任何事件
PS:關於事件分發機制的深入研究,最好還是從源碼著手。以上測試例子,僅僅從結果入手,並沒有做到“知其所以然”。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.