RecyclerView自定义滚动条

ViewPager2通过反射可以获取到RecyclerView,也可以使用。

效果如图:
在这里插入图片描述

参考代码:

// 下面代码参考 https://github.com/LateNightMoon/ScrollbarSample
public class VerticalScrollbar extends View {

    private static final String TAG = "VerticalScrollbar";
    private static final int START = 0x1;
    private static final int END = START << 1;
    private static final int SCROLLING = START << 2;
    private static final long DELAY_CLEAR = 1000L;
    private int mScrollState = START;
    private final Rect trackRect = new Rect();
    private Drawable trackBackground = null;
    private Drawable thumbBackground = ResourcesCompat.getDrawable(getResources(), R.drawable.scroller, null);
    private float thumbScale = 0f;
    private float scrollScale = 0f;
    private RecyclerView mRecyclerView;

    private volatile boolean isCleared = false;

    private int leastCount = 4;

    private final RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                removeCallbacks(clearCanvasRunner);
                isCleared = false;
                computeScrollScale();
            } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                removeCallbacks(clearCanvasRunner);
                postDelayed(clearCanvasRunner, DELAY_CLEAR);
            }
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            computeScrollScale();
            postInvalidate();
        }
    };


    public VerticalScrollbar(Context context) {
        this(context, null);
    }

    public VerticalScrollbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VerticalScrollbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_HARDWARE, null);
    }

    public void bindRecyclerView(@Nullable RecyclerView recyclerView) {
        if (mRecyclerView == recyclerView) {
            return;
        }
        mRecyclerView = recyclerView;
        if (mRecyclerView != null) {
            setCallbacks();
        }
    }

    private void setCallbacks() {
        mRecyclerView.addOnScrollListener(mScrollListener);
        mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                computeScrollScale();
            }
        });
        computeScrollScale();
    }

    public void computeScrollScale() {
        if (mRecyclerView == null) {
            return;
        }
        float scrollExtent = mRecyclerView.computeVerticalScrollExtent();
        float scrollRange = mRecyclerView.computeVerticalScrollRange();
        if (scrollRange != 0 && scrollExtent != 0) {
            thumbScale = scrollExtent / scrollRange;
        }
        float offset = mRecyclerView.computeVerticalScrollOffset();
        if (offset != 0 && scrollRange != 0) {
            scrollScale = offset / scrollRange;
        }
        float toEndExtent = scrollRange - scrollExtent;
        if (offset == 0) {
            mScrollState = START;
        } else if (BigDecimal.valueOf(toEndExtent).equals(BigDecimal.valueOf(offset))) {
            mScrollState = END;
        } else {
            mScrollState = SCROLLING;
        }
        postInvalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureHandler(widthMeasureSpec), measureHandler(heightMeasureSpec));
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            isCleared = false;
            invalidate();
            removeCallbacks(clearCanvasRunner);
            postDelayed(clearCanvasRunner, DELAY_CLEAR);
        }
    }

    private int measureHandler(int measureSpec) {
        int result = 0;
        int size = MeasureSpec.getSize(measureSpec);
        int mode = MeasureSpec.getMode(measureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            result = Math.max(size, result);
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isCleared || !hasContent()) {
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            return;
        }
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (trackBackground != null) {
            trackRect.set(0, 0, width, height);
            trackBackground.setBounds(trackRect);
            trackBackground.draw(canvas);
        }
        int thumbTop = (int) (scrollScale * height);
        int thumbBottom = (int) (thumbTop + height * thumbScale);
        // 添加 state 是修复滚动计算差
        if (thumbBackground != null) {
            switch (mScrollState) {
                case START:
                    trackRect.set(0, 0, width, thumbBottom);
                    break;
                case SCROLLING:
                    trackRect.set(0, thumbTop, width, thumbBottom);
                    break;
                case END:
                    trackRect.set(0, thumbTop, width, height);
                    break;
                default:
            }
            thumbBackground.setBounds(trackRect);
            thumbBackground.draw(canvas);
        }
    }

    private final Runnable clearCanvasRunner = () -> {
        isCleared = true;
        invalidate();
    };

    @SuppressWarnings("rawtypes")
    private boolean hasContent() {
        if (mRecyclerView == null) {
            return false;
        }
        RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
        if (adapter == null) {
            return false;
        }
        int itemCount = adapter.getItemCount();
        return itemCount > leastCount;
    }

    public void setLeastCount(int leastCount) {
        this.leastCount = leastCount;
    }
}

要实现 RecyclerView 滚动条高度自定义,你可以考虑使用一个自定义滚动条 View,然后通过监听 RecyclerView 的滚动事件来控制滚动条的位置和高度。 首先,你需要在 RecyclerView 的布局文件中添加一个自定义滚动条 View,例如: ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.example.customscrollbar.CustomScrollBar android:id="@+id/custom_scroll_bar" android:layout_width="10dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_marginRight="10dp" /> </RelativeLayout> ``` 其中,CustomScrollBar 是自定义滚动条 View,它的宽度可以根据需求自行设置。 然后,在 RecyclerView 的代码中,你需要监听 RecyclerView 的滚动事件,计算滚动条的位置和高度,然后更新 CustomScrollBar 的位置和高度。例如: ```java recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 计算当前可见的 item 数量 int visibleItemCount = recyclerView.getLayoutManager().getChildCount(); // 计算所有 item 的数量 int totalItemCount = recyclerView.getLayoutManager().getItemCount(); // 计算第一个可见的 item 的位置 int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); // 计算滚动条的高度 int scrollBarHeight = (int) ((float) visibleItemCount / (float) totalItemCount * recyclerView.getHeight()); // 计算滚动条的位置 int scrollBarTop = (int) ((float) firstVisibleItemPosition / (float) totalItemCount * recyclerView.getHeight()); // 更新滚动条的位置和高度 customScrollBar.setScrollBarTop(scrollBarTop); customScrollBar.setScrollBarHeight(scrollBarHeight); } }); ``` 在这段代码中,我们使用了 RecyclerView 的 LayoutManager 中的方法来计算当前可见的 item 数量、所有 item 的数量和第一个可见的 item 的位置,然后根据这些数据计算出滚动条的位置和高度,最后更新 CustomScrollBar 的位置和高度。 最后,你需要在 CustomScrollBar 中实现 setScrollBarTop() 和 setScrollBarHeight() 方法,用于设置滚动条的位置和高度。例如: ```java public void setScrollBarTop(int top) { setY(top); } public void setScrollBarHeight(int height) { getLayoutParams().height = height; requestLayout(); } ``` 这样,你就可以实现 RecyclerView 滚动条高度自定义的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值