[Android][Java]RcyclerView快速滚动

RecyclerView 快速滚动是一种允许用户快速浏览和导航长列表的功能。 它通常以滚动条的形式出现在 RecyclerView 的一侧,用户可以通过拖动滚动条来快速跳转到列表的不同部分。 

主要特点

  • 快速导航: 用户无需手动滚动整个列表,即可快速到达所需位置。 
  • 滚动条: 快速滚动通常以滚动条的形式呈现,显示在 RecyclerView 的一侧(通常是右侧)。
  • 字母索引(可选): 某些快速滚动实现还包括字母索引,允许用户点击字母直接跳转到以该字母开头的项目。 
  • 预览: 一些高级实现会在用户拖动滚动条时显示当前位置的项目预览。 

实现方式

RecyclerView 的快速滚动功能主要由 FastScroller 类实现。 你可以通过以下方式启用和自定义快速滚动:

<androidx.recyclerview.widget.RecyclerView
  android:id="@+id/recyclerview_result"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_marginTop="4dp"
  android:paddingEnd="31dp"
  android:layout_marginStart="31dp"
  app:fastScrollEnabled="true"
  app:fastScrollVerticalTrackDrawable="@color/transparent"
  app:fastScrollVerticalThumbDrawable="@drawable/vf_line_drawable"
  app:fastScrollHorizontalTrackDrawable="@color/transparent"
  app:fastScrollHorizontalThumbDrawable="@drawable/vf_line_drawable"
  app:layout_constraintTop_toBottomOf="@id/nav_bar"
  app:layout_constraintBottom_toBottomOf="parent">
</androidx.recyclerview.widget.RecyclerView>

其中关于快速滚动的参数解析如下:

  • fastScrollEnabled:boolean 类型,决定是否启用快速滚动,当设置为 true 时需要设置下面的四个属性。
  • fastScrollHorizontalThumbDrawable:水平滚动块。
  • fastScrollHorizontalTrackDrawable:水平滚动栏背景。
  • fastScrollVerticalThumbDrawable:竖直滚动块。
  • fastScrollVerticalTrackDrawable:竖直滚动栏背景。

自定义滚动块drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:state_pressed="true"
		android:drawable="@drawable/hf_line_touched"/>
	<item
		android:drawable="@drawable/hf_line"/>
</selector>

其中android:state_pressed="true"代表着是滚动块被点击滑动时的样式;下面则是滚动条正常跟随recyclerVIew滚动的样式。

滚动块被点击滑动时的样式

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:left="3dp"
		android:top="20dp"
		android:right="10dp"
		android:bottom="20dp">
		<shape>
			<size android:width="18dp"/>
			<solid android:color="#C5D0DE" />
			<corners android:radius="100dp" />
		</shape></item>
</layer-list>

通过layer-list来创建一个包含 shape 的 Drawable,并在 layer-list 中设置 shape 的 padding 属性,从而间接实现外边距的效果。从而保持滚动条的大小保持一致

正常样式

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
	<item
		android:left="13dp"
		android:top="20dp"
		android:right="10dp"
		android:bottom="20dp">
		<shape>
			<size android:width="8dp"/>
			<solid android:color="#C5D0DE" />
			<corners android:radius="100dp" />
		</shape></item>
</layer-list>

同上面一致,13 + 8 + 10 = 3 + 18 +10 = 31dp的滚动条宽度, 而右外边距保持10dp不变。从而实现点按滚动条后,滚动条宽度从8dp向左增宽到18dp的功能。

总结:

RecyclerView 快速滚动是一种提升用户体验的重要功能,它允许用户快速浏览和导航长列表。 通过启用 FastScroller 并自定义其样式,你可以轻松地将快速滚动功能添加到你的应用程序中。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值