As I indicated in my answer here
https://stackoverflow.com/questions/52964701/how-to-make-auto-smooth-scroll-with-offset-recycler-view/53036053?noredirect=1#comment93059650_53036053
I will publish that I have the same as the answer to this question
Thank you @woesss for the link to this answer.
https://stackoverflow.com/a/39654328
The result was like this
My custom LinnearLayoutManager
public class SmoothLayoutManager extends LinearLayoutManager { public static final int X_25 = 25; public static final int X_200 = 200; public static final float DEFAULT = X_25; /** * !! IMPORTANT !! * If you need to add new value, don't forget add it here also */ @Retention(RetentionPolicy.SOURCE) @IntDef({X_25, X_200}) private @interface Speed { } private static float MILLISECONDS_PER_INCH = DEFAULT; public SmoothLayoutManager(Context context) { super(context); } public SmoothLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public SmoothLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public SmoothLayoutManager setSpeedOfSmooth(@Speed int iSpeed) { MILLISECONDS_PER_INCH = iSpeed; return this; } @Override public void scrollToPositionWithOffset(final int position, final int offset) { super.scrollToPositionWithOffset(position, offset); } @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition); } @Override protected int getVerticalSnapPreference() { return LinearSmoothScroller.SNAP_TO_ANY; } @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; } @Override public int calculateDtToFit(final int viewStart, final int viewEnd, final int boxStart, final int boxEnd, final int snapPreference) { return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2); } }; smoothScroller.setTargetPosition(position); startSmoothScroll(smoothScroller); } }
And this is how it is applied.
private void setRv(Context iC) { RecyclerView.Adapter adapter = new UpSaleInnerAdapter(mPicasso, mInflater, iLink -> mListener.onButtonClick(iLink)); mRv.setLayoutManager(new SmoothLayoutManager(iC, LinearLayoutManager.HORIZONTAL, false).setSpeedOfSmooth(SmoothLayoutManager.X_200)); mRv.setAdapter(adapter); SnapHelper snapHelper = new LinearSnapHelper(); snapHelper.attachToRecyclerView(mRv); }
Note:
I noticed that if you quickly swipe your finger across the screen, it will confuse SnapHelper and it will scroll further than necessary. Say, I do a swipe, of course, that somewhere around 10 cells will slip through and I will see the content, but in fact the scrolling continues on + 10 more cells. And this турбо режим becomes noticeable.
If someone came across this and knows what it is about, tell me if you can fix it
Thank!