简体   繁体   中英

android.view.MotionEvent.ACTION_UP not detected when scollview kicks in

I have two Touch-able TextViews put inside a ScrollView

 <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>

And the onTouchEvent function as below

 @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;
 }

When the page is within the size of the screen (eg the page doesn't need to scroll), everything works fine. We could detect the android.view.MotionEvent.ACTION_UP, regardless of where we touch and lift our hand.

However when the page size is bigger than the screen size, the page scrolling detection kicks in when the hand touch and move. During this time, if we begin with touch on the TextView, the android.view.MotionEvent.ACTION_DOWN could be detected. However as we move (ie the scrolling happening) the android.view.MotionEvent.ACTION_UP is not detected when the touch is lift up.

How could I enable my android.view.MotionEvent.ACTION_UP still detectable even when the scrolling effect takes it? Or is there a way to capture that the user have lift up the finger after scrolling action kicks in (if MotionEvent.ACTION_UP is not possible to triggered)? Thanks!

After some exploration, found a solution to my problem. The problem is android.view.MotionEvent.ACTION_UP is now on the ScrollView. So to solve the problem, I add an id to the ScrollView (snippet of the code above)

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

Then, I register ScrollView to OnTouchListener as well. And detect it when the TOUCH is UP as below

@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;
}

With that, I could perform the needed function whenever ACTION_UP is done, regardless from the TextView or ScrollView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM