Question

I have a Scroller is used for a fling. I initialize it with an initial velocity and some limits. When I later get the new Y value, getCurrY returns the Y limit passed into fling.

Here's heavily simplified code:

public class SimpleList extends ScrollView implements Runnable
{
  private VelocityTracker velocityTracker = null; // initialization redacted
  private Scroller scroller = null;

  @Override
  public boolean onTouchEvent(MotionEvent ev)
  {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK)
    {
      case MotionEvent.ACTION_DOWN:
        velocityTracker.addMovement (ev);
        return true;

      case MotionEvent.ACTION_MOVE:
      {
        velocityTracker.addMovement (ev);
        return true;
      }

      case MotionEvent.ACTION_UP: 
        velocityTracker.computeCurrentVelocity (1000);
        int index = ev.getActionIndex();
        int pointerId = ev.getPointerId(index);
        int velY = (int) VelocityTrackerCompat.getYVelocity (velocityTracker, pointerId);
        velocityTracker.recycle();

        scroller = new Scroller (getContext());
        scroller.fling (0, 0, 0, velY,
                        Integer.MIN_VALUE, Integer.MIN_VALUE,
                        Integer.MAX_VALUE, Integer.MAX_VALUE);
        postDelayed (this, 100);
        return true;
    }
    return false;
  }

  public void run()
  {
    if (scroller.isFinished())
      return;

    if (scroller.computeScrollOffset())
    {
      System.out.println ("curVel " + scroller.getCurrVelocity());
      System.out.println ("newY " + scroller.getCurrY());
    }
  }
}

The initial velocity seems reasonable - around 1000 px/sec. The logged velocity is a bit less, which also seems reasonable. Yet, the "newY" value is the MAX_VALUE passed into fling.

Any ideas? Any ideas?

Était-ce utile?

La solution

One has to use OverScroller.

The documentation for Scroller wasn't clear to me - I thought that the min and max values were only used to limit the amount of scroll. However they are also used to determine the scroll range, meaning that computeScrollOffset uses the limits to scale the animation, not just to limit it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top