I have a horizontal RecyclerView that automatically scrolls content every 3 seconds.

Did as described here

https://stackoverflow.com/questions/35773980/recyclerview-auto-scroll-to-display-all-the-elements-as-in-news-feed-etc

Everything worked fine as long as the cell width was on the entire bus of the screen.

Now I need to make the cell smaller in size than the width of the screen and I did it like this

enter image description here

It does not look beautiful because the cell is not in the middle.

I need the cell to stop automatically before the end with the automatic svaype. That it was like this

enter image description here

End of previous - central - next cell

It turns out that in the method described by the link above

 @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { return SmoothLayoutManager.this.computeScrollVectorForPosition(targetPosition); } @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; } }; linearSmoothScroller.setTargetPosition(position); startSmoothScroll(linearSmoothScroller); } 

I can control the speed of the svayp animation, but I can’t set the offset

There is another method

 scrollToPositionWithOffset(final int position, final int offset) 

And he can do the offset I need, but he doesn’t animate the svayp

As an animation from the first method to apply with the offset parameter from the second method

Any ideas speak

  • stackoverflow.com/a/39654328 - and tried it? - woesss
  • @woesss yes, that's what I need. Post this answer and I will mark it as correct. Thank! - Aleksey Timoshchenko

1 answer 1

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!