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