I had implemented a smooth scroll animation for ViewPager using OnjectAnimator. Now, i am trying to do the same for a recyclerView but the scroll animation is abrupt. Here is my animation using ObjectAnimator for a viewpager. It produces a snap animation to demonstrate the scroll capability of the viewpager.
ObjectAnimator objectAnimator = ObjectAnimator
.ofFloat(new PagerHintMovement(-10, viewPager), "progress", -1f, 1f);
objectAnimator.setInterpolator(new AccelerateInterpolator());
objectAnimator.setDuration(1500);
objectAnimator.setRepeatCount(1);
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.i(TAG, "Animation: Starting fake drag on view pager.");
viewPager.beginFakeDrag();
}
@Override
public void onAnimationEnd(Animator animation) {
Log.i(TAG, "Animation: Ending fake drag on view pager.");
viewPager.endFakeDrag();
viewPager.setCurrentItem(viewPager.getCurrentItem());
}
@Override
public void onAnimationCancel(Animator animation) {
viewPager.setCurrentItem(viewPager.getCurrentItem());
}
@Override
public void onAnimationRepeat(Animator animation) {
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
if (animation.getInterpolator() instanceof AccelerateInterpolator) {
animation.setInterpolator(new DecelerateInterpolator());
} else {
animation.setInterpolator(new AccelerateInterpolator());
}
}
}
});
objectAnimator.setStartDelay(1000);
objectAnimator.start();
Any suggestions on how to achieve the same for a recyclerView ?
If you are not satisfied with default RecyclerView.smoothScrollToPosition(int)
you will need to do some class overrides.
Smooth scroll in RecyclerView
is triggered by calling
LayoutManager.startSmoothScroll(RecyclerView.SmoothScroller)
on its current layoutManager
.
You are free to override LayoutManager
and provide custom RecyclerView.SmoothScroller
implementation - preferably by extending RecyclerView.LinearSmoothScroller
.