要实现自动循环水平滚动的 RecyclerView,你可以使用 LoopingLayoutManager 类来替代 LinearLayoutManager。

首先,在你的项目中添加以下依赖项:

implementation 'com.github.wangjiegulu:RecyclerViewLayoutManager:1.2.8'

然后,在你的布局文件中添加 RecyclerView:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

接下来,在你的 Activity 或 Fragment 中,初始化 RecyclerView 并设置 LoopingLayoutManager:

val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
val layoutManager = LooperLinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.layoutManager = layoutManager

// 添加你的数据适配器
val adapter = MyAdapter(data)
recyclerView.adapter = adapter

// 设置自动循环滚动
layoutManager.setAutoLooperEnable(true)
layoutManager.setAutoLooperInterval(3000) // 设置自动循环间隔时间,单位为毫秒

现在,你的 RecyclerView 将会自动循环水平滚动。

Android Kotlin: 实现 RecyclerView 自动循环水平滚动

原文地址: https://www.cveoy.top/t/topic/bTdx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录