1 回答
TA贡献1797条经验 获得超4个赞
我做了如下:
为 RecyclerView 创建自动滚动
`
private fun autoScroll() {
scrollCount = 0;
var speedScroll: Long = 1200;
val runnable = object : Runnable {
override fun run() {
if (layoutManager.findFirstVisibleItemPosition() >= imageArrayList.size / 2) {
adapter.load()
}
recyclerView.smoothScrollToPosition(scrollCount++)
Log.e(TAG, "run: $scrollCount")
handler.postDelayed(this, speedScroll)
}
}
handler.postDelayed(runnable, speedScroll)
}
`
自动创建平滑滚动
`
layoutManager = object : LinearLayoutManager(this@MainActivity) {
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?, position: Int) {
val smoothScroller = object : LinearSmoothScroller(this@MainActivity) {
override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {
return 5.0f;
}
}
smoothScroller.targetPosition = position
startSmoothScroll(smoothScroller)
}
}
`
对于源代码,查看 GitHub 项目链接 https://github.com/Mahesh2318/AutoScrollRecyclerView
添加回答
举报