How to auto scroll RecyclerView
smoothly so that user can see all the elements of the RecyclerView and scroll again from the start - as in News Feed etc.
Simplified in Kotlin:
lifecycleScope.launch(Dispatchers.Main, start = CoroutineStart.DEFAULT) {
var position = MINIMUM_POSITION
repeat(AUTO_SCROLL_REPEATING_TIMES) {
delay(SCROLL_DELAY)
rvBanners.smoothScrollToPosition(position)
when {
position+1 == listSize -> position = 0
position == 0 -> position = MINIMUM_POSITION
else -> position++
}
}
}
If you want to start the auto-scroll at a specific time:
val job = lifecycleScope.launch(Dispatchers.Main, start = CoroutineStart.LAZY) {
var position = MINIMUM_POSITION
repeat(AUTO_SCROLL_REPEATING_TIMES) {
delay(SCROLL_DELAY)
rvBanners.smoothScrollToPosition(position)
when {
position+1 == listSize -> position = 0
position == 0 -> position = MINIMUM_POSITION
else -> position++
}
}
}
...
job.start()