Smooth scrolling recyclerview android

I have Recyclerviewwhich contains ratingbarinlist_items.xml

upon completion of the evaluation of one element in RecyclerviewI will automatically switch to another elementRecyclerview

Scrolling is very fast; the user cannot know that it is scrolling.

I implemented smooth scrolling, but did not work properly, the first scroll of the 2nd element is smooth (the first is already displayed), but scrolling scrolls faster

LinearLayoutSmoothScroll.java

public class LinearLayoutSmoothScroll extends LinearLayoutManager {
        private static final float MILLISECONDS_PER_INCH = 100f;
        private Context mContext;

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

        public LinearLayoutSmoothScroll(Context context, int orientation, boolean reverseLayout) {
                super(context, orientation, reverseLayout);
                mContext=context;
        }

        public LinearLayoutSmoothScroll(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
                super(context, attrs, defStyleAttr, defStyleRes);
                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 LinearLayoutSmoothScroll.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);
        }
}

using:

layoutManager.smoothScrollToPosition(rvSurvey,new RecyclerView.State(),new_position);
+3
source share

All Articles