How can i control the scrolling speed of recyclerView.smoothScrollToPosition(position)

前端 未结 5 614
感情败类
感情败类 2020-11-28 23:18

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 .... And i m able to do this by<

5条回答
  •  庸人自扰
    2020-11-28 23:54

    I've made a version that you can set the scrolling speed variable.

    If you set factor to 2, it will be twice as slow as default.

    public class VariableScrollSpeedLinearLayoutManager extends LinearLayoutManager {
    
        private final float factor;
    
        public VariableScrollSpeedLinearLayoutManager(Context context, float factor) {
            super(context);
            this.factor = factor;
        }
    
        @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 VariableScrollSpeedLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
                }
    
                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return super.calculateSpeedPerPixel(displayMetrics) * factor;
                }
            };
    
            linearSmoothScroller.setTargetPosition(position);
            startSmoothScroll(linearSmoothScroller);
        }
    }
    

提交回复
热议问题