OverScroller fling doesn't always finish animation where it's supposed to

470 views Asked by At

I'm using the code below to implement a vertical fling in a custom view. mViewsHeight is just an offset which helps calculate the maxY in the fling and is a correct calculation. mViewsTop is a variable which helps me keep track of the children views and their relative ypositions. Most of the time, the fling works as expected. However, on some flings, I've noticed that it doesn't finish where getFinalY() says it should. For instance, the minY will be set to 0 and the finalY will also equal 0, but for some reason the views won't always settle with the top view in line with the top of the parent view. I debugged it thoroughly and I can see that this works sometimes and doesn't others. I am not obstructing the animation in any way; it just seems to not get to where it's supposed to.

Has anyone else experienced something similar and does anyone have a solution for how to ensure that the fling animation will end where it's intended to (i.e. where getFinalY() says it should)?

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        int maxY;
        if (mViewsHeight > getHeight()) {
            maxY = mViewsHeight - getHeight();
            int startY = getScrollY();
            scroller.fling(0, startY, 0, (int) -velocityY, 0, 0, 0, maxY,
                    0, 30);
            int finalY = scroller.getFinalY();
            mViewsTop = -finalY;
        }
        return true;
    }

@Override
public void computeScroll() {
    super.computeScroll();
    if (scroller.computeScrollOffset()) {
        int currX = scroller.getCurrX();
        int currY = scroller.getCurrY();
        boolean canScrollY = (mViewsTop < 0 || getChildAt(0).getBottom() > getHeight());
        if (canScrollY) {
            scrollTo(currX, currY);
        }
    }
}
0

There are 0 answers