简体   繁体   English

scollview启动时未检测到android.view.MotionEvent.ACTION_UP

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

I have two Touch-able TextViews put inside a ScrollView 我在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>

And the onTouchEvent function as below 以及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;
 }

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. 无论我们在何处触摸并举起手,我们都可以检测到android.view.MotionEvent.ACTION_UP。

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. 在此期间,如果我们先触摸TextView,则可以检测到android.view.MotionEvent.ACTION_DOWN。 However as we move (ie the scrolling happening) the android.view.MotionEvent.ACTION_UP is not detected when the touch is lift up. 但是,当我们移动(即发生滚动)时,抬起触摸时未检测到android.view.MotionEvent.ACTION_UP。

How could I enable my android.view.MotionEvent.ACTION_UP still detectable even when the scrolling effect takes it? 即使滚动效果生效,如何启用仍可检测到的android.view.MotionEvent.ACTION_UP? 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)? 还是有一种方法可以捕捉到用户在滚动动作开始后抬起了手指(如果无法触发MotionEvent.ACTION_UP)? Thanks! 谢谢!

After some exploration, found a solution to my problem. 经过一番探索,找到了解决我问题的方法。 The problem is android.view.MotionEvent.ACTION_UP is now on the ScrollView. 问题是android.view.MotionEvent.ACTION_UP现在位于ScrollView上。 So to solve the problem, I add an id to the ScrollView (snippet of the code above) 因此,为了解决该问题,我向ScrollView添加了一个ID(上面的代码片段)

<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. 然后,我也将ScrollView注册到OnTouchListener。 And detect it when the TOUCH is UP as below 并在触摸到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;
}

With that, I could perform the needed function whenever ACTION_UP is done, regardless from the TextView or ScrollView. 这样,无论从TextView还是ScrollView,无论何时执行ACTION_UP,我都可以执行所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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