java - 是否有任何库可以让我们在 recyclerview 中获得选取框效果(例如 hotstar vip 页面滚动图像)?
问题描述
块引用
我正在尝试实现像hotstar这样的自动滚动无限recyclerview,在hotstar VIP订阅页面中有它。
我试过给定的代码。
对于自动滚动回收器:-
binding.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
推荐阅读
- cordova - 版本控制如何在 ionic 中发生
- ansible - 使用 ansible 的 maven_artifact 进行证书验证
- elasticsearch - Elasticsearch - JSON 字段映射爆炸
- python - 如何将一列中的值匹配到具有多个值的第二列
- php - 如何将 127.0.0.1/index.php 的 url 设置为 127.0.0.1?
- angular - i18n 翻译带间距的动态文本
- javascript - 如何从 Navigator 之外的某个地方导航?
- sql - 汇总金额平均
- c - 我不断收到分段错误,我不知道为什么
- r - 错误“'x' 的所有条目必须是非负的和有限的”