custom_thumb.xml
<shape
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@android:color/holo_red_light" />
<size
android:width="40dp"
android:height="10dp" />
</shape>
seekbar_drawable.xml
<layer-list>
<item android:id="@android:id/progress">
<clip android:drawable="@android:color/transparent" />
</item>
</layer-list>
seekbar_background.xml
<shape>
<corners android:radius="20dp"/>
<solid android:color="@android:color/darker_gray" />
</shape>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@drawable/seekbar_background"
app:layout_constraintBottom_toBottomOf="@id/seekBar"
app:layout_constraintEnd_toEndOf="@id/seekBar"
app:layout_constraintStart_toStartOf="@id/seekBar"
app:layout_constraintTop_toTopOf="@id/seekBar" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:max="10000"
android:min="0"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:progressDrawable="@drawable/seekbar_drawable"
android:thumb="@drawable/custom_thumb"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用 SeekBar.OnSeekBarChangeListener来滚动RecyclerView,使用RecyclerView的滚动函数。当手动滚动RecyclerView时,使用 RecyclerView.OnScrollListener来设置seekbar的进度。您需要建立两个视图滚动状态之间的映射。val thumb = binding.seekBar.thumb as GradientDrawable
thumb.setSize(thumb.intrinsicWidth * 2, thumb.intrinsicHeight)
您可以创建自己的自定义滚动条,并将其与回收视图绑定。 以下是用于处理上述制作的滚动条的回收视图扩展方法
fun RecyclerView.handleScroll(itemScrollerBinding: ItemScrollerBinding) {
post {
val parentWidth = itemScrollerBinding.parent.width
val layoutParams = itemScrollerBinding.progress.layoutParams
val initialX = itemScrollerBinding.progress.x
layoutManager?.let {
val totalWidth = this.computeHorizontalScrollRange()
val visibleWidth = this.computeHorizontalScrollExtent()
val percent =
visibleWidth.toFloat() / totalWidth.toFloat()
layoutParams.width = (parentWidth * percent).toInt()
itemScrollerBinding.progress.layoutParams = layoutParams
addOnScrollListener(object :
RecyclerView.OnScrollListener() {
override fun onScrolled(
recyclerView: RecyclerView,
dx: Int,
dy: Int,
) {
super.onScrolled(recyclerView, dx, dy)
val scrolledWidth =
this@handleScroll.computeHorizontalScrollOffset()
val updatedVisibleWidthRecyclerView =
visibleWidth + scrolledWidth
val scrolledWidthScroller =
((parentWidth.toFloat() / totalWidth) * scrolledWidth)
itemScrollerBinding.progress.x =
initialX + scrolledWidthScroller
}
})
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="@dimen/size_48"
android:layout_height="@dimen/size_6"
android:background="@drawable/bg_scroller_background">
<LinearLayout
android:id="@+id/progress"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@drawable/bg_scroller"
android:orientation="horizontal" />
</FrameLayout>
您可以将此放置在回收视图下方并居中显示。