Android LinearSnapHelper - how to increase scrolling/"snapping" speed?

android, android-recyclerview, linearlayoutmanager

Solution

As 郭玉龙 mentioned, SnapHelper call RecyclerView.smoothScrollBy() method. And it use default sQuinticInterpolator. To change speed of snap you can do next:

public class SlowdownRecyclerView extends RecyclerView {

// Change pow to control speed.
// Bigger = faster. RecyclerView default is 5.
private static final int POW = 2;

private Interpolator interpolator;

public SlowdownRecyclerView(Context context) {
    super(context);
    createInterpolator();
}

public SlowdownRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    createInterpolator();
}

public SlowdownRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    createInterpolator();
}

private void createInterpolator(){
    interpolator = new Interpolator() {
        @Override
        public float getInterpolation(float t) {
            t = Math.abs(t - 1.0f);
            return (float) (1.0f - Math.pow(t, POW));
        }
    };
}

@Override
public void smoothScrollBy(int dx, int dy) {
    super.smoothScrollBy(dx, dy, interpolator);
}

Or you can implement your own interpolator.

Problem

I'm using a `LinearSnapHelper` to make items in my `RecyclerView` "snap" into place on the screen (my cards take up most of the screen, so I want them to snap into place and fill the screen on every swipe/fling/scroll). I'm struggling with how to make the cards snap into place faster. I've tried creating a custom `LinearLayoutManager` (and editing the `calculateSpeedPerPixel` method in `scrollToPosition` or `smoothScrollToPosition`), as well as a custom `RecyclerView` (and editing the fling method). But nothing effects the speed that cards "snap" into place. I suppose the issue is that I don't really understand how `LinearSnapHelper` "scrolls" the cards into position. It doesn't seem to use `LinearLayoutManager`'s `scrollToPosition` or `smoothScrollToPosition` methods. ``` snapHelper = new LinearSnapHelper() { @Override public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) { View centerView = findSnapView(layoutManager); if (centerView == null) { return RecyclerView.NO_POSITION; } int position = layoutManager.getPosition(centerView); int targetPosition = -1; if (layoutManager.canScrollHorizontally()) { if (velocityX < 0) { targetPosition = position - 1; } else { targetPosition = position + 1; } } if (layoutManager.canScrollVertically()) { if (velocityY > 0) { targetPosition = position + 1; } else { targetPosition = position - 1; } } final int firstItem = 0; final int lastItem = layoutManager.getItemCount() - 1; targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem)); return targetPosition; } }; snapHelper.attachToRecyclerView(mRecyclerView); ```

Original source

Related problems