Scroll up a ScrollView slowly

前端 未结 5 581
小蘑菇
小蘑菇 2020-12-09 18:31

The question is \"How do i scroll up a ScrollView to top very smoothly and slowly\".

In my special case i need to scroll to top in about 1-2 seconds. Ive tried inter

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 18:56

    Try the following code:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        ValueAnimator realSmoothScrollAnimation = 
            ValueAnimator.ofInt(parentScrollView.getScrollY(), targetScrollY);
        realSmoothScrollAnimation.setDuration(500);
        realSmoothScrollAnimation.addUpdateListener(new AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                int scrollTo = (Integer) animation.getAnimatedValue();
                parentScrollView.scrollTo(0, scrollTo);
            }
        });
    
        realSmoothScrollAnimation.start();
    }
    else
    {
        parentScrollView.smoothScrollTo(0, targetScrollY);
    }
    

提交回复
热议问题