簡體   English   中英

scollview啟動時未檢測到android.view.MotionEvent.ACTION_UP

[英]android.view.MotionEvent.ACTION_UP not detected when scollview kicks in

我在ScrollView中放置了兩個可觸摸的TextView

 <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:isScrollContainer="false" 
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >    

        <TextView 
            android:id="@+id/restReviewRateText"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="@string/rest_review_rate_text"/>
        <TextView
            android:id="@+id/restReviewRateTextInc" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="@string/rest_review_rate_text_inc"/>
    </LinearLayout>
 </ScrollView>

以及onTouchEvent函數如下

 @Override
 public boolean onTouch(View v, MotionEvent event) {
     switch (v.getId()) {
     case R.id.restReviewRateText:
     case R.id.restReviewRateTextInc:
         if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
            Log.d("MY TAG", "I am here DOWN!!");
         } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
            Log.d("MY TAG", "I am here UP!!");

     }
         break;
     }
     return false;
 }

當頁面在屏幕大小范圍內時(例如,頁面無需滾動),一切正常。 無論我們在何處觸摸並舉起手,我們都可以檢測到android.view.MotionEvent.ACTION_UP。

但是,如果頁面大小大於屏幕大小,則在觸摸和移動手時會進行頁面滾動檢測。 在此期間,如果我們先觸摸TextView,則可以檢測到android.view.MotionEvent.ACTION_DOWN。 但是,當我們移動(即發生滾動)時,抬起觸摸時未檢測到android.view.MotionEvent.ACTION_UP。

即使滾動效果生效,如何啟用仍可檢測到的android.view.MotionEvent.ACTION_UP? 還是有一種方法可以捕捉到用戶在滾動動作開始后抬起了手指(如果無法觸發MotionEvent.ACTION_UP)? 謝謝!

經過一番探索,找到了解決我問題的方法。 問題是android.view.MotionEvent.ACTION_UP現在位於ScrollView上。 因此,為了解決該問題,我向ScrollView添加了一個ID(上面的代碼片段)

<ScrollView
   android:id="@+id/reviewScrollView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:isScrollContainer="false" 
   >

然后,我也將ScrollView注冊到OnTouchListener。 並在觸摸到UP時進行檢測,如下所示

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (v.getId()) {
    case R.id.restReviewRateText:
    case R.id.restReviewRateTextInc:
        if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
           Log.d("MY TAG", "I am here DOWN!!");
        } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
           Log.d("MY TAG", "I am here UP!!");

        }
        break;
    case R.id.reviewScrollView:
        if (event.getAction() == android.view.MotionEvent.ACTION_UP)
           Log.d("MY TAG", "I am here UP!!");
        break;
    }
    return false;
}

這樣,無論從TextView還是ScrollView,無論何時執行ACTION_UP,我都可以執行所需的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM