如何为RecyclerView制作自定义滚动条的Android实现

5

我为RecyclerView自定义了垂直滚动条,但是用于显示项目位置的拇指有点太大并且在顶部时也有偏移。

在styles.xml中,我定义了使用thumb.xml和track.xml文件的自定义滚动条以实现自定义轨道和拇指。在recyler.xml中,我添加了一个样式属性来显示自定义滚动条,并设置填充。

有什么建议来解决这个问题吗?我尝试更改thumb.xml中的宽度和高度,但无效。谢谢!

样式.xml文件:

<resources>
<style name="AppTheme" parent="@style/Theme.Leanback">
    <item name="@attr/scrollbarStyle">@style/scrollbar_shape_style</item>
</style>

<attr name="scrollbarStyle" format="reference"/>

<style name="scrollbar_shape_style">
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
    <item name="android:scrollbarStyle">outsideOverlay</item>
    <item name="android:scrollbars">vertical</item>
    <item name="android:fadeScrollbars">false</item>
    <item name="android:scrollbarThumbVertical">@drawable/thumb</item>
    <item name="android:scrollbarTrackVertical">@drawable/track</item>
    <item name="android:scrollbarSize">8dp</item>
</style>

thumb.xml 文件:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
    android:angle="0"
    android:endColor="#ffffff"
    android:startColor="#ffffff" />

<corners android:radius="5dp" />
<size android:width="8dp" />
<size android:height="3dp" />

track.xml 文件:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient
        android:angle="0"
        android:endColor="#9BA3C5"
        android:startColor="#8388A4" />
    <stroke
        android:width="7dp"
        android:color="#00ffffff"/>
</shape>

回收站的XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="383dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        style="@style/scrollbar_shape_style"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingEnd="50dp"
        android:paddingStart="0dp"/>
</LinearLayout>

尝试这个:- https://dev59.com/x1sW5IYBdhLWcg3wFj9p - ND1010_
谢谢,我尝试了,但拇指还是一样的,我仍然有高度问题。 - alezniki
@alezniki:你尝试过以编程方式设置setSmoothScrollbarEnabled - false吗? - Radhey
是的,我做了,谢谢,但还是不起作用。 - alezniki
是的,下面的代码完全正常,现在它随着项目滚动。 - alezniki
显示剩余4条评论
1个回答

4
我已经将滚动条上的拇指图标大小固定了。以下是修复大小的thumb.xml文件。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="top">
    <shape android:shape="rectangle">
        <corners android:radius="100dp" />
        <solid android:color="#e9eef2" />
        <size
            android:width="5dp"
            android:height="25dp" />
    </shape>
</item>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接