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
添加回答
舉報