解決android中HorizontalScrollView的滾動事件與組件的Touch衝突問題

來源:互聯網
上載者:User
 

在上一章中我們實現了抽屜+滾動功能,但是遺留了一個問題就是滾動事件與組件的Touch事件衝突,接下來我們看一下他們衝突的原因

public boolean onInterceptTouchEvent (MotionEvent ev)

使用此方法可以攔截所有觸控螢幕動作引發的事件。這意味著你可以監視指派給子項的事件,並且可以拿到任何當前手勢的所有權。

使用此方法需謹慎。因為它與View.onTouchEvent(MotionEvent)有相當複雜的互動影響。這兩者都必須同時正確地實現。事件將按以下順序來被方法接收:

   1. 接收到down事件

   2. 事件將被視圖組的一個子視圖處理,或者被傳遞給自己的onTouchEvent()方法處理;這意味著你必須實現onTouchEvent(),並且返回true,這樣才可以接著接受到其他的手勢(而不是尋求一個父視圖來處理它)。onTouchEvent()返回true後,你將不再接受到onInterceptTouchEvent()的任何事件,同時所有對觸摸動作的處理必須像往常一樣在onTouchEvent()中進行。

   3. 如果返回false,則接下來的每個事件(所有的up事件,包含最後一個up)將會首先被傳遞到這裡,然後到目標對象view的onTouchEvent()。

   4. 如果返回ture,你將不會接收到以下任何事件:目標view將會接收到相同的事件,但是帶著ACTION_CANCEL的動作。所有在此之後的事件將會被傳遞到你的onTouchEvent()方法中,並且不再在這裡出現。

參數

ev    沿著樹型結構往下指派的動作事件

         傳回值

             若將動作事件從子視圖中截獲並通過onTouchEvent()將他們指派給當前ViewGroup,則返回true。當前目標將收到一個ACTION_CANCEL事件,並且不再會有其他訊息被傳遞到這裡。

 

onInterceptTouchEvent 這個功能只有在布局中才會有的,如上面所述,所以我們解決這個問題,就要實現當我們實現組件的Touch事件的時候,需要onInterceptTouchEvent 返回false,否則組件無法捕獲Touch事件,知道原理以後,我們就可以編寫代碼,我們使用一個標誌位來標準HorizontalScrollView的子View是否正在實現Touch事件,如果有則設定flag
= 0;當Touch_up的時候,設定flag = -1,而在HorizontalScrollView的onInterceptTouchEvent 函數中判斷flag是否等於-1,如果flag == -1,則onInterceptTouchEvent 返回return super.onInterceptTouchEvent(ev);,否則返回false。下面我們使用代碼來實現:

布局檔案需要重新編寫:

<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"    tools:context=".MainActivity" >    <SlidingDrawer        android:id="@+id/slidingDrawer1"        android:layout_width="wrap_content"        android:layout_height="80dp"android:layout_alignParentBottom="true"        android:content="@+id/content"        android:handle="@+id/handle" >        <Button            android:id="@+id/handle"            android:layout_width="wrap_content"            android:layout_height="20dp"            android:text="Handle" />        <com.example.test.Horizon            android:id="@+id/content"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:background="@android:color/black"            android:scrollbars="none" >            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="50dp"                android:layout_gravity="center_vertical"                android:gravity="center_vertical"                android:id="@+id/content_linear"                android:orientation="horizontal" >                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:id="@+id/btn1"                    android:text="btn1" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn2" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn3" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn4" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>                <Button                    android:layout_width="70dp"                    android:layout_height="50dp"                    android:layout_marginLeft="10dp"                    android:text="btn5" >                </Button>            </LinearLayout>        </com.example.test.Horizon>    </SlidingDrawer></RelativeLayout>

與上面上一章中的布局檔案不同的地方就是用com.example.test.Horizon替換HorizontalScrollView,下面就是Horizon類的內容:

Horizon.java

 

package com.example.test;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.HorizontalScrollView;public class Horizon extends HorizontalScrollView{public Horizon(Context context) {super(context);}public Horizon(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {if (MainActivity.flag == -1) {return super.onInterceptTouchEvent(ev);}else{return false;}}}

主類的內容如下:

package com.example.test;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn1 = (Button)findViewById(R.id.btn1);btn1.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:flag = 0;Log.e("test", "down");break;case MotionEvent.ACTION_MOVE:Log.e("test", "move");break;case MotionEvent.ACTION_UP:flag = -1;Log.e("test", "up");break;}return false;}});}public Button btn1;public static int flag = -1;}

測試結果如下:

07-06 21:49:04.639: E/test(409): down07-06 21:49:04.659: E/test(409): move07-06 21:49:04.669: E/test(409): move07-06 21:49:04.679: E/test(409): move07-06 21:49:04.699: E/test(409): move07-06 21:49:04.709: E/test(409): move07-06 21:49:04.719: E/test(409): move07-06 21:49:04.739: E/test(409): move07-06 21:49:04.749: E/test(409): move07-06 21:49:04.769: E/test(409): move07-06 21:49:04.769: E/test(409): up

上面組件btn1實現了Touch事件同時移動,但是能夠測試到up事件,不過你移動組件的時候,是不能移動捲軸

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.