Search code examples
androidandroid-recyclerviewsmooth-scrolling

How to perform an eased scroll using RecyclerView


I have a RecyclerView setup with a horizontal LinearLayoutManager that displays views in full page size (i.e. like a ViewPager would).

What I would like to achieve is to programmatically smooth scroll to a certain position. However not with a linear scroll like smoothScrollToPosition(int) does, but with an ease-in/ease-out effect instead.

I looked at RecyclerView.SmoothScroller but from what I could tell there is no way to implement this behaviour, because information about the total scroll distance/duration seems to be missing.

The next thing I tried was to use scrollTo(int, int) manually which seems to be unsupported in RecyclerView.

I have for now resorted to sending MotionEvents to simulate a drag on the recylcer view. But this feels very overkill for what should be a simple problem.

Is there another way to achieve my goal?


Solution

  • You can try with creating custom RecyclerView and overriding smoothScrollBy method:

    public class CustomInterpolatorRecyclerView extends RecyclerView {
    
    @Override
    public void smoothScrollBy(int dx, int dy) {
        super.smoothScrollBy(dx, dy, new AccelerateDecelerateInterpolator()); //pass your more advanced ease-in/ease-out interpolator here
    }