background

In the project, want to make RecyclerView slowly gently sliding specified position, so use:

RecyclerView.smoothScrollToPosition(int);Copy the code

SmoothScrollToPosition is not smooth as the function name, smoothScrollToPosition is not smooth, black question marks??

In this paper, the original author MichaelX, nuggets link: https://juejin.cn/user/3016715634743479, reprint please indicate the source.Copy the code

adventure

If the function name is a smooth, smooth slide to a specified position, why is it not ideal? View the source code as follows:

Public void smoothScrollToPosition(int Position) {// ··· omit non-related code, MLayout object is the RecyclerView LayoutManager mLayout. SmoothScrollToPosition (this, mState, position); }Copy the code

So is actually called RecyclerView. LayoutManager. SmoothScrollToPosition () method, this is an abstract method. Since the author’s project is a LinearLayoutManager, the concrete implementation is found as follows:

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

            LinearSmoothScroller smoothScroller = new LinearSmoothScroller(context);

            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }Copy the code

Generate a RecyclerView. SmoothScroller SmoothScroller LinearSmoothScroller subclass object, then use SmoothScroller slide to complete the rest of the work.

So go inside the LinearSmoothScroller. Significant discovery — there is a function related to sliding speed:

/** * Calculates the scroll speed. * Calculate the scroll speed * Return: the amount of time needed to slide through 1px. */ protectedfloatCalculateSpeedPerPixel (DisplayMetrics DisplayMetrics) {// MILLISECONDS_PER_INCH is constant and equal to 20Freturn MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
    }Copy the code

Since RecyclerView. SmoothScrollToPosition (int); Very soon, is it possible to extend the sliding time?

SmoothScrollToPosition is invalid

To verify the idea of extending the sliding time in the previous section, customize a LinearLayoutManager:

public class SmoothScrollLayoutManager extends LinearLayoutManager { public SmoothScrollLayoutManager(Context context) {  super(context); } @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, Final int Position) {LinearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {// Return: Elapsed time (ms) while gliding 1px. @Override protectedfloat calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                            return150f / displayMetrics.densityDpi; }}; smoothScroller.setTargetPosition(position); startSmoothScroll(smoothScroller); }}Copy the code

Binggo! Success! Call RecyclerView. SmoothScrollToPosition (int); It’s a lot slower, it’s no longer abrupt, it’s no longer abrupt, there’s no transition, it’s just a slow slide, it’s finally smooth.

In this paper, the original author MichaelX, nuggets link: https://juejin.cn/user/3016715634743479, reprint please indicate the source.Copy the code

conclusion

So by customizing the LinearLayoutManager, Overwrite the calculateSpeedPerPixel(DisplayMetrics) method of the LinearSmoothScroller object in the smoothScrollToPosition() method to recyclerView.smoothscrol lToPosition(int); Slide smoothly and smoothly to the specified position. Other LayoutManagers are not available for the time being, so you need to try it yourself.

If readers have other ideas, please leave a message.