使用自定义RecyclerView实现,先看效果,能用得上的再继续往下看……
上代码~
自定义AutoRollRecyclerView,继承RecyclerView,重点只有这一个类
class AutoRollRecyclerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : RecyclerView(context, attrs) {
companion object {
private const val TIME_AUTO_ROLL: Long = 16L //隔多长时间移动一次
}
private val autoPollTask by lazy { AutoRollTask() }
private var running: Boolean = false //是否正在自动轮询
private var canRun: Boolean = false//是否可以自动轮询
inner class AutoRollTask : Runnable {
private val distanceOnce = 1 //每次滚动距离设置
private var isScrollToEnd = false //是否滚动到了最右边
override fun run() {
val recyclerView = this@AutoRollRecyclerView
if (recyclerView.running && recyclerView.canRun) {
//当前显示区域的宽度(屏幕从左到右可见区域) + 横向滚动偏移量 >= recyclerView的整体横向宽度(包含屏幕外的)
if (recyclerView.computeHorizontalScrollExtent() + recyclerView.computeHorizontalScrollOffset() >= recyclerView.computeHorizontalScrollRange()) {
isScrollToEnd = true
} else if (recyclerView.computeHorizontalScrollOffset() == 0) {
isScrollToEnd = false
}
//横向滚动,只修改x即可
recyclerView.scrollBy(if (isScrollToEnd) -distanceOnce else distanceOnce, 0)
//每隔TIME_AUTO_ROLL滚动一次
recyclerView.postDelayed(recyclerView.autoPollTask, TIME_AUTO_ROLL)
}
}
}
/**
* 开始
*/
fun start() {
if (running)
stop()
canRun = true
running = true
postDelayed(autoPollTask, TIME_AUTO_ROLL)
}
/**
* 停止
*/
fun stop() {
running = false
removeCallbacks(autoPollTask)
}
override fun onTouchEvent(e: MotionEvent): Boolean {
when (e.action) {
MotionEvent.ACTION_DOWN -> { //当点击按下时,停止滚动
if (running)
stop()
}
MotionEvent.ACTION_UP,
MotionEvent.ACTION_CANCEL,
MotionEvent.ACTION_OUTSIDE -> { //松开时,继续滚动
if (canRun)
start()
}
}
return super.onTouchEvent(e)
}
}
下面是Demo示例代码
布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- com.demo.application.widget 是AutoRollRecyclerView所在包-->
<com.demo.application.widget.AutoRollRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>
每个item布局:item_topic.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.AppCompatTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:layout_marginVertical="3dp"
android:background="@drawable/shape_bg_topic"
android:paddingHorizontal="10dp"
android:paddingVertical="4dp"
android:textColor="#00A89C"
android:textSize="12sp"
tools:text="# 书中自有浓情意浓情意" />
适配器TopicAdapter
/**
* BaseQuickAdapter是第三方库BRVAH里的类(https://github.com/CymChad/BaseRecyclerViewAdapterHelper)
* 可以在主module的build.gradle里引入
* implementation 'io.github.cymchad:BaseRecyclerViewAdapterHelper:4.0.1'
*/
class TopicAdapter : BaseQuickAdapter<String, QuickViewHolder>() {
override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: String?) {
holder.setText(R.id.tvName, item)
}
override fun onCreateViewHolder(
context: Context,
parent: ViewGroup,
viewType: Int
): QuickViewHolder {
return QuickViewHolder(R.layout.item_topic, parent)
}
}
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val adapter by lazy { TopicAdapter() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
//横向Staggered布局
binding.recyclerView.layoutManager =
StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL)
//设置adapter
binding.recyclerView.adapter = adapter
//填装数据
adapter.submitList(
arrayListOf(
"# 书中自有颜如玉",
"# 举头望明月",
"# 贴贴",
"# 哈哈哈哈哈哈",
"# 极限运动",
"# 请回答1988",
"# 健身达人",
"# LOL",
"# 德玛西亚",
"# 我的地盘听我的",
"# 哼哼哈嘿"
)
)
}
override fun onResume() {
super.onResume()
binding.recyclerView.start()
}
override fun onPause() {
super.onPause()
binding.recyclerView.stop()
}
}
▲新手小备注:Activity里使用了viewBinding,记得在模块build.gradle里开启支持
android {
……
viewBinding {
enabled = true
}
}
结束~