Android Kotlin RecyclerView 自动循环滚动实现:快速滚动、点击、触摸停止、松开继续
要实现 RecyclerView 的自动循环滚动,可以使用以下步骤:
- 创建一个自定义的 RecyclerViewLayoutManager 类,继承自 LinearLayoutManager 或 GridLayoutManager,重写 canScrollHorizontally() 和 canScrollVertically() 方法,让 RecyclerView 可以水平或垂直滚动。
class CustomLayoutManager : LinearLayoutManager() {
private var isScrollEnabled = true
fun setScrollEnabled(scrollEnabled: Boolean) {
isScrollEnabled = scrollEnabled
}
override fun canScrollHorizontally(): Boolean {
return isScrollEnabled && super.canScrollHorizontally()
}
override fun canScrollVertically(): Boolean {
return isScrollEnabled && super.canScrollVertically()
}
}
- 在 RecyclerView 的 Adapter 中,使用 Handler 和 Runnable 实现自动循环滚动功能。在 Adapter 的 onAttachedToRecyclerView() 方法中启动自动滚动。
class CustomAdapter(private val dataList: List<String>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
private var recyclerView: RecyclerView? = null
private val handler = Handler()
private var currentPosition = 0
private var isAutoScroll = true
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
startAutoScroll()
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
this.recyclerView = null
stopAutoScroll()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindData(dataList[position])
holder.itemView.setOnClickListener {
// 处理点击事件
}
}
override fun getItemCount(): Int {
return dataList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindData(data: String) {
// 绑定数据到视图
}
}
private val autoScrollRunnable = object : Runnable {
override fun run() {
if (isAutoScroll) {
currentPosition++
if (currentPosition >= dataList.size) {
currentPosition = 0
}
recyclerView?.smoothScrollToPosition(currentPosition)
handler.postDelayed(this, 3000) // 3秒后继续滚动
}
}
}
private fun startAutoScroll() {
isAutoScroll = true
handler.postDelayed(autoScrollRunnable, 3000)
}
private fun stopAutoScroll() {
isAutoScroll = false
handler.removeCallbacks(autoScrollRunnable)
}
}
- 在 Activity 或 Fragment 中,将自定义的 LayoutManager 和 Adapter 应用到 RecyclerView 上。
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val layoutManager = CustomLayoutManager()
recyclerView.layoutManager = layoutManager
val adapter = CustomAdapter(dataList)
recyclerView.adapter = adapter
- 如果需要在触摸 RecyclerView 时停止滚动,可以监听 RecyclerView 的触摸事件,在触摸开始时停止自动滚动,在触摸结束时继续滚动。
recyclerView.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
layoutManager.setScrollEnabled(false) // 停止滚动
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
layoutManager.setScrollEnabled(true) // 继续滚动
}
}
false
}
这样就实现了可以自动循环滚动、可以快速滚动、滚动中可点击、触摸停止滚动、松开继续滚动的 RecyclerView。
原文地址: https://www.cveoy.top/t/topic/bnCM 著作权归作者所有。请勿转载和采集!