Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Smooth)ScrollToPosition doesn't work properly with RecyclerView

People also ask

How do I make my RecyclerView scroll smooth?

You need to use the "Picasso" library to load image asynchronously, and also make sure that the size of the image which you load is similar to the size of image view in the layout, using the fit() function. Also, if you are using a recycler view inside the scroll view, make sure to set nested scrolling to false.

What is RecyclerView LayoutManager?

A RecyclerView.LayoutManager implementation that lays out items in a grid for leanback VerticalGridView and HorizontalGridView . LinearLayoutManager. A RecyclerView.LayoutManager implementation which provides similar functionality to ListView .

What is RecyclerView state?

androidx.recyclerview.widget.RecyclerView.State. Contains useful information about the current RecyclerView state like target scroll position or view focus. State object can also keep arbitrary data, identified by resource ids. Often times, RecyclerView components will need to pass information between each other.


Finally I was able to make it work! LinearLayoutManager.scrollToPositionWithOffset(int, int) did the trick.


I also have same issue, but managed to fix the issue by Customizing SmoothScroller

let Custom LayoutManager as below

public class CustomLayoutManager extends LinearLayoutManager {
    private static final float MILLISECONDS_PER_INCH = 50f;
    private Context mContext;

    public CustomLayoutManager(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView,
        RecyclerView.State state, final int position) {

        LinearSmoothScroller smoothScroller = 
            new LinearSmoothScroller(mContext) {

            //This controls the direction in which smoothScroll looks
            //for your view
            @Override
            public PointF computeScrollVectorForPosition
            (int targetPosition) {
                return CustomLayoutManager.this
                    .computeScrollVectorForPosition(targetPosition);
            }

            //This returns the milliseconds it takes to 
            //scroll one pixel.
            @Override
            protected float calculateSpeedPerPixel
                (DisplayMetrics displayMetrics) {
                return MILLISECONDS_PER_INCH/displayMetrics.densityDpi;
            }
        };

        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
}

(documentation commented inside the code given above)Please set the above LayoutManager to the recyerview

CustomLayoutManagerlayoutManager = new CustomLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.smoothScrollToPosition(position);

by using the custom Layout manager

scrollToPosition also working well in my case u can use

recyclerView.scrollToPosition(position)

also if you want to adjust the speed of smoothScrollToPosition please override the

private static final float MILLISECONDS_PER_INCH = 50f;

in CustomLayoutManager. So if we put the value as 1f the smoothScrollToPosition will be faster like scrollToPosition.increasing value make delay and decreasing will make the speed of scroll. Hope this will useful.


In My case,

`mRecyclerView.scrollToPosition(10);` 

also did not work. But

`mRecyclerView.smoothScrollToPosition(10);` 

works fine for me...


To scroll down smoothly to bottom from any position in the RecyclerView on clicking EditText.

edittext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rv_commentList.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                      rv_commentList.scrollToPosition(rv_commentList.getAdapter().getItemCount() - 1);
                    }
                }, 1000);
            }
        });

Another reason why any of the before mentioned solutions may not work is if your RecyclerView is embedded in a NestedScrollView. In this case you have to call the scroll action on the NestedScrollView.

for example:

nestedScrollview.smoothScrollTo(0,0)