自定义RecyclerView滚动条样式

在安卓项目中,对RecyclerView设置自定义滚动条样式时遇到滚动条不显示的困扰。通过检查XML布局和style代码,发现将`android:overScrollMode`设置为`View.OVER_SCROLL_NEVER`后,滚动条可以正常显示。同时,注意在Android 26及以上版本,使用原生fastScroll设置不会受到影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RecyclerView自定义ScroolBar样式后,ScrollBar不出现

最近做项目,需要对RecyclerView设置自定义scroolBar样式,可是滚动条死活不出现
代码如下activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <yiyou.yinhui.sh.cn.myapplication.fastscroll.views.FastScrollRecyclerView
        android:id="@+id/recyclerView_search_keyword_result_list"
        style="@style/ZoomItemRecyclerView_style"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:fastScrollTrackColor="#60606D"
        app:fastScrollThumbInactiveColor="#008EE3"
        />
</RelativeLayout>

引用的style代码

 <style name="RecyclerView_style">
        <item name="android:layout_marginRight">10dp</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:scrollbarSize">2dp</item>
        <item name="android:overScrollMode">never</item>
    </style>

刚开始我以为是自定义样式的锅,好,用原生的总该没问题了吧。结果,还是不出现

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_search_keyword_result_list"
        style="@style/ZoomItemRecyclerView_style"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        />
</RelativeLayout>

看代码并没有问题是吧。于是我开始百度,查找RecyclerView的滚动条不出现的原因。然而并没有发现有跟我出现类似的问题的答案。没办法,网上找不到,那就一个一个排除吧,最后发现把ZoomItemRecyclerView_style中

 <item name="android:overScrollMode">never</item>

去掉就能够正常显示了,那么如果要设置android:overScrollMode的属性怎么弄了。我们可以在代码里面动态的设置setOverScrollMode(View.OVER_SCROLL_NEVER);

注意
android 26以上,RecyclerView就支持了fastScroll,用原生的fastScroll设置的时候不受影响。

项目地址
recyclerView_scrollbar.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值