0

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 ?

1
  • Is it OnjectAnimator or ObjectAnimator as stated in the question's third line? Commented May 11, 2019 at 17:37

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Pawel. I basically want something like scrollBy (x, y). I tried translationX using ObjectAnimator but it's very abrupt.
Can you show how to override LayoutManager with custom smooth scroller implementation?
0

As you mentioned you need smooth scroll you may follow the following steps:

  1. Hide the recycler view and display a progress bar there
  2. Create a background asynchronous task where you setup and load the recycler view
  3. While setting data in the recycler adapter use callbacks to retrieve the data from your data model
  4. On recycler setup complete hide the progress bar and set the recycler view visible.

This will make your recycler smoothly scrollable and decent loading.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.