Blockquote
我正在尝试实现自动滚动无限recyclerview,就像hotstar在hotstar VIP订阅页面中那样。
我已经尝试过给定的代码。
对于自动滚动回收器:-
绑定(bind).rvPartyOfWeek.addOnScrollListener(CustomScrollListener())
private val SCROLLING_RUNNABLE = object : Runnable {
override fun run() {
val duration = 10
val pixelsToMove = 22
if (!isPartyOfWeekScrolling) {
binding.rvPartyOfWeek.smoothScrollBy(pixelsToMove, 0)
}
mHandler.postDelayed(this, duration.toLong())
}
}
对于无限滚动:-
binding.rvPartyOfWeek.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView2: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView2, dx, dy)
val totalItemCount = layoutManagerparty.itemCount
val lastVisibleItem = layoutManagerparty.findLastVisibleItemPosition()
if (totalItemCount <= (lastVisibleItem + 3)) {
if (totalItemCount > 22) {
for (i in 0..10) {
listParty.removeAt(0)
}
}
listParty.addAll(listPartySingle)
adapterpartyofweek.notifyDataSetChanged()
Log.i("Helllo listParty", listParty.size.toString())
}
}
})
在某些设备中滚动不流畅,在某些旧设备中崩溃。
最佳答案
我做了如下操作:
- 为 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
关于java - 是否有任何库可以让我们在 recyclerview 中获得选框效果(例如 hotstar vip 页面滚动图像)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56250445/