Android自定义View——RecyclerView 滚动条

一、效果

最近闲来无事,手动撸个滚动条,看看效果:
竖直 水平

GIF 有些失帧……

二、导入

Step 1. 添加 jitpack 仓库:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Step 2. 添加依赖

dependencies {
	implementation 'com.github.cathu:SmartScrollBar:+'
}

三、已实现和未实现的功能(持续更新)

    <declare-styleable name="SmartScrollBar">
        <!-- ✅  方向 -->
        <attr name="smart_orientation" format="enum">
            <enum name="horizontal" value="0"/>
            <enum name="vertical" value="1"/>
        </attr>
        <!-- ✅  background 圆角(只支持 Android 5.0 以上的版本) -->
        <attr name="smart_background_corner" format="dimension"/>
        <!-- ✅  sliderBar 圆角 -->
        <attr name="smart_slider_corner" format="dimension"/>
        <!-- ✅  sliderBar 颜色 -->
        <attr name="smart_slider_color" format="color"/>
        <!-- ✅  RecyclerView 无法滑动时,scroll 的状态 -->
        <attr name="smart_cant_scroll_style" format="enum">
            <enum name="dismiss_invisible" value="0"/>  <!-- 消失(invisible) -->
            <enum name="dismiss_gone" value="1"/>   <!-- 消失(gone) -->
            <enum name="visible" value="2"/>    <!-- 显示 scroll -->
        </attr>
        <!-- ✅ 滑块风格 -->
        <attr name="smart_slider_style" format="enum">
            <enum name="倍数缩放" value="0"/>
            <enum name="固定大小" value="1"/>
        </attr>
        <!-- ✅  滑块长度 -->
        <attr name="smart_slider_length" format="fraction|dimension"/>
        <!-- ❌  滑块图片 -->
        <attr name="smart_slider_img" format="reference"/>
        <!-- ✅  滑块隐藏风格 -->
        <attr name="smart_can_scroll_style" format="enum">
            <enum name="always_show" value="0"/>
            <enum name="dismiss_awhile" value="1"/>
        </attr>
        <!-- ✅  sliderBar 多少毫秒(ms)后消失 -->
        <attr name="smart_dismiss_time" format="integer"/>
        <!-- ✅ 支持拖拽 sliderBar -->
        <attr name="smart_enable_drag" format="boolean" />
    </declare-styleable>

支持自定义背景:

scrollBar.setCustomBackground(holderWH: (Int,Int) -> RectF, corner: Float, @ColorInt color: Int)

holderWH会返回宽高,利用宽高你可以创建一个 RectF 对象并返回给 SmartScrollBar,他会构建一个长方形背景。利用这个你可以实现背景的宽度比滑块的宽度小的场景。

四、代码地址

主类 SmartScrollBar 目前只有不到 300行代码,很适初学自定义View的同学学习。

GitHub:https://github.com/cathu/SamrtScrollBar

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
要实现 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 滚动条高度自定义的效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值