上图先:
这个东西叫ScrollBar,具体的动画形式是当用户上下或者左右滑动界面时,这个滑块会在需要显示的位置滑动。
以这张图为例,我们来做一个在屏幕右侧滑动的进度条。
首先,我们需要先画出它的shape——打开res资源文件夹底下的drawable文件夹,右击——New——Drawable Resource File,输入文件名,例如my_scrollBar_shape,其余默认。
此时就会出现一个默认为layer-list的xml资源文件。
这个文件的作用就是画出图片中的灰色渐变长矩形,也就是我们的滑块。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 规定长宽-->
<item
android:width="@dimen/dp_4"
android:height="@dimen/dp_94" >
<!-- 绘制形状为矩形-->
<shape android:shape="rectangle">
<!-- 绘制梯度为线性,灰色渐变色-->
<gradient
android:type="linear"
android:useLevel="true"
android:startColor="#ffa0a8b9"
android:endColor="#ff565d6b"
android:angle="90"
/>
<!-- 四个折角的位置-->
<corners
android:topLeftRadius="0dp"
android:topRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
/>
</shape>
</item>
</layer-list>
如代码,这个滑块我们已经做好了,可以在xml文件的design里面看到缩略图
那么下一步,我们就需要把这个滑块应用到我们的界面上去,打开你需要添加右侧滑块的layout文件,找到RecyclerView或者ListView:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
//如下是我们添加的属性
android:scrollbars="vertical" //垂直滑动
android:scrollbarThumbVertical="@drawable/my_scrollBar_shape" //添加滑块形状
android:fadeScrollbars="false" //无触控时不隐藏,如果设置为true就会隐藏
/>
到这里就完成了。
参考博客:
RecyclerView自带的滚动条