RecyclerView循環滾動,無限滾動

RecyclerView 實現背景 或者 item 循環滾動,無限滾動
先看效果:
在這裏插入圖片描述
之後有時間了,再上傳一下,自定義imageview 實現大圖長圖循環滾動的view;

自動滾動實現方法:首先自定義LinearLayoutManager,重寫smoothScrollToPosition方法,然後實現個自己的LinearSmoothScroller ,在calculateSpeedPerPixel返回個自定義的速率值。看過源碼就會很容易理解,這裏就不上源碼了,可以自行搜索一下,循環滾動的話adapter裏 getItemCount() 返回集合size,如果是一個方法無限滾動下去,就返回 Integer.MAX_VALUE;代碼裏
recyclerView.smoothScrollToPosition(Integer.MAX_VALUE / 2);就不需要監聽滾動了。

代碼如下:

public class CustomLinerLayoutManager extends LinearLayoutManager {
    private float speedPerPixel = 0.001f;//滾動速率 越小越快 越大越慢
    private Context context;

    public CustomLinerLayoutManager(Context context) {
        super(context);
        this.context = context;
    }

    public CustomLinerLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public CustomLinerLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    public void setSpeedPerPixel(float speedPerPixel) {
        this.speedPerPixel = speedPerPixel;
    }

    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    private class CenterSmoothScroller extends LinearSmoothScroller {

        CenterSmoothScroller(Context context) {
            super(context);
        }

        @Nullable
        @Override //滑動到目標索引
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return CustomLinerLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override // 滑動位置
        public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
            return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
        }

        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {// 控制滑動速度的
            return speedPerPixel;
        }

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }

    public void setSpeedSlow() {
        speedPerPixel = context.getResources().getDisplayMetrics().density * 3f;
    }

    public void setSpeedFast() {
        speedPerPixel = context.getResources().getDisplayMetrics().density * 0.03f;
    }
}

RecyclerView 添加 OnScrollListener 監聽,滑動過程中,監聽當前的索引位置 或者 在頂部或者是在底部,方法很多,可以參考實現 或者自行是實現,在底部了就讓它往頂部滾動,在頂部了就讓它往底部滾動,速率大小可以通過CustomLinerLayoutManager 自定義 ,寫個方法外部set 進去也可以。

 RecyclerView.OnScrollListener onScrollListenerTwo = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            isVisBottom(recyclerView);
        }
    };

    public void isVisBottom(RecyclerView recyclerView) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        if (!recyclerView.canScrollHorizontally(-1)) {//當前在頂部
            recyclerView.smoothScrollToPosition(linearLayoutManager.getItemCount() - 1);
        }
        if (!recyclerView.canScrollHorizontally(1)) {//當前在底部
            recyclerView.smoothScrollToPosition(0);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章