RecyclerVIew auto scroll to display all the elements as in News Feed etc.,

前端 未结 7 732
慢半拍i
慢半拍i 2020-12-14 11:47

How to auto scroll RecyclerView smoothly so that user can see all the elements of the RecyclerView and scroll again from the start - as in News Feed etc.

<
7条回答
  •  没有蜡笔的小新
    2020-12-14 12:38

    Just to improve on the answer a little, it's auto scrolling infinity with smooth animation.

    final int speedScroll = 1200;
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        int count = 0;
        boolean flag = true;
        @Override
        public void run() {
            if(count < adapter.getItemCount()){
                if(count==adapter.getItemCount()-1){
                    flag = false;
                }else if(count == 0){
                    flag = true;
                }
                if(flag) count++;
                else count--;
    
                recyclerView.smoothScrollToPosition(count);
                handler.postDelayed(this,speedScroll);
            }
        }
    };
    
    handler.postDelayed(runnable,speedScroll);
    

    This is exactly your answer but if you link to more smooth animation then use LayoutManager

    recyclerView.setLayoutManager(new CustomLinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
    

    Control you animation changing MILLISECONDS_PER_INCH value.

    public class CustomLinearLayoutManager extends LinearLayoutManager {
        public CustomLinearLayoutManager(Context context) {
            super(context);
        }
    
        public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
    
        public CustomLinearLayoutManager(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()) {
                        private static final float MILLISECONDS_PER_INCH = 200f;
    
                        @Override
                        public PointF computeScrollVectorForPosition(int targetPosition) {
                            return CustomLinearLayoutManager.this
                                    .computeScrollVectorForPosition(targetPosition);
                        }
    
                        @Override
                        protected float calculateSpeedPerPixel
                                (DisplayMetrics displayMetrics) {
                            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                        }
                    };
            linearSmoothScroller.setTargetPosition(position);
            startSmoothScroll(linearSmoothScroller);
        }
    }
    

提交回复
热议问题