【问题标题】:Is there anyway i can detect a scroll and scroll stop events ?无论如何我可以检测到滚动和滚动停止事件吗?
【发布时间】:2014-09-30 14:07:59
【问题描述】:

我在布局中使用滚动视图。我需要在有滚动事件时制作动画,并在滚动结束时停止动画。

我检查了滚动视图,但它似乎没有提供这些事件。我需要使用手势检测器吗?

请帮忙。

谢谢, 斯内哈

【问题讨论】:

    标签: android scroll event-handling scrollview


    【解决方案1】:

    这解决了我的问题! :)

    svMovieDetails.setOnTouchListener(new View.OnTouchListener(){

            @Override
            public boolean onTouch( View v, MotionEvent event ) {
                switch ( event.getAction( ) ) {
                    case MotionEvent.ACTION_SCROLL:
                    case MotionEvent.ACTION_MOVE:
                        Log.e( "SCROLL", "ACTION_SCROLL" );
                        break;
                    case MotionEvent.ACTION_DOWN:
                        Log.e( "SCROLL", "ACTION_DOWN" );
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        Log.e( "SCROLL", "SCROLL_STOP" );
                        break;
                }
                return false;
            }
        } );
    

    【讨论】:

      【解决方案2】:

      你可以扩展ScrollView,并覆盖

      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt) 
      

      获取卷轴。要在您的活动/片段中获取此值,您还可以定义一个侦听器

      【讨论】:

      • 谢谢。但是如果卷轴正在发生或结束,它是如何得到的呢?
      • 我认为oldt== toldl==l时可以考虑滚动空闲。如果发生,值将不同
      【解决方案3】:
      import android.view.GestureDetector;
      import android.view.GestureDetector.SimpleOnGestureListener;
      import android.view.MotionEvent;
      import android.view.View;
      import android.view.View.OnTouchListener;
      
      public class OnSwipeTouchListener implements OnTouchListener {
      
          private final GestureDetector gestureDetector;
      
          public OnSwipeTouchListener (Context ctx){
              gestureDetector = new GestureDetector(ctx, new GestureListener());
          }
      
          private final class GestureListener extends SimpleOnGestureListener {
      
              private static final int SWIPE_THRESHOLD = 100;
              private static final int SWIPE_VELOCITY_THRESHOLD = 100;
      
              @Override
              public boolean onDown(MotionEvent e) {
                  return true;
              }
      
              @Override
              public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                  boolean result = false;
                  try {
                      float diffY = e2.getY() - e1.getY();
                      float diffX = e2.getX() - e1.getX();
                      if (Math.abs(diffX) > Math.abs(diffY)) {
                          if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                              if (diffX > 0) {
                                  onSwipeRight();
                              } else {
                                  onSwipeLeft();
                              }
                          }
                          result = true;
                      } 
                      else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                              if (diffY > 0) {
                                  onSwipeBottom();
                              } else {
                                  onSwipeTop();
                              }
                          }
                          result = true;
      
                  } catch (Exception exception) {
                      exception.printStackTrace();
                  }
                  return result;
              }
          }
      
          public void onSwipeRight() {
          }
      
          public void onSwipeLeft() {
          }
      
          public void onSwipeTop() {
          }
      
          public void onSwipeBottom() {
          }
      }
      

      这可能是我从this 讨论中得到的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-01
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多