我是用recycleView实现的,每隔30毫秒调用一次smoothScrollBy(x,y)方法就可以了。
private fun startScroll() {
Observable.interval(30, TimeUnit.MILLISECONDS)
.takeUntil { stopScoll }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
rvMarquee.smoothScrollBy(15, 0)
}
}
因为我这个页面每次onResume的时候都要刷新数据,所以在请求数据成功后调用startScroll(),并将stopScroll置为false。然后在onPause()里将stopScroll置为true就可以了。因为跑马灯是无限跑的,但数据是不是很多,所以我直接将数据复制了1000份,这样跑马灯在短时间内就不会停止了。
override fun onGetTaskCenterSuccess(taskcenter: TaskCenter) {
val marqueeRecords = taskcenter.marqueeRecords
val mutableList = mutableListOf<Marquee>()
if (!marqueeRecords!!.isEmpty()) {
(0..1000).forEach {
mutableList.addAll(marqueeList)
}
}
if (marqueeAdapter.data == null)
marqueeAdapter.setNewData(mutableList)
else
marqueeAdapter.replaceData(mutableList)
stopScoll = false
startScroll()
}
override fun onPause() {
super.onPause()
stopScoll = true
}