I have a recycler view, and I want a smooth scrolldown and then scrollup to it programatically to show the complete content in it to user.
I can do this by:
final int height=recyclerView.getChildAt(0).getHeight(); recyclerView.smoothScrollToPosition(height); recyclerView.postDelayed(new Runnable() { public void run() { recyclerView.smoothScrollToPosition(0); } },200);
But what I want is to slow down the scrolling speed, so that the complete content gets visible clearly.
I can do this by: final int height=recyclerView. getChildAt(0). getHeight(); recyclerView.
Just to improve on the answer a little:
public class SpeedyLinearLayoutManager extends LinearLayoutManager { private static final float MILLISECONDS_PER_INCH = 5f; //default is 25f (bigger = slower) public SpeedyLinearLayoutManager(Context context) { super(context); } public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) { super(context, orientation, reverseLayout); } public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { return super.computeScrollVectorForPosition(targetPosition); } @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; } }; linearSmoothScroller.setTargetPosition(position); startSmoothScroll(linearSmoothScroller); } }
And then set SpeedyLayoutManager to your RecyclerView:
recyclerView.setLayoutManager(new SpeedyLinearLayoutManager(context, SpeedyLinearLayoutManager.VERTICAL, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With