I am stuck in a scenario where i have to make a infinite scroll recycler view(in both directions up and down in my case) I have some static data(5-8 items in the list) and no pagination is there an at a time only one item is displayed on the screen like a vertical View Pager. Any help/suggestions are appreciated. Thanks in Advance :)
Asked
Active
Viewed 104 times
0
-
Can you add some code please – jayesh gurudayalani May 29 '23 at 12:47
-
Use this as reference https://stackoverflow.com/a/13787543/8547372 this will help you – Dharmeet Soni May 30 '23 at 05:43
-
Thanks a lot but I've implemented the solution @DharmeetSoni – shivam chawla May 30 '23 at 10:41
1 Answers
0
Got the solution by myself Posting the answer so that it may help others in not wasting their time
to make the infinite recyclerview in both directions, make the following changes in your adapter and activity:
In Adapter: Fool the RV by modifying the getItemsCount method
override fun getItemCount() =
yourList?.let { if (it.size > 1) Int.MAX_VALUE else it.size } ?: 0
Modify on bind method to get correct position
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(yourList?.let { if (it.size != 0) position % it.size else 0 } ?: 0)
}
Scroll to the mid of the items ( as now your list is the size of Int.Max_value -not actually don't worry this is a recycler view) after setting your adapter:
binding.yourRV.scrollToPosition(Int.MAX_VALUE / 2)
To move to the First Position:
binding.yourRV.scrollToPosition(
(Int.MAX_VALUE / 2) - (Int.MAX_VALUE / 2) % yourList.size
)
To Move to any specific or last saved position:
val firstPos = (Int.MAX_VALUE / 2) - (Int.MAX_VALUE / 2) % yourList.size
binding.yourRV.scrollToPosition( firstPos + indexYouWishToMoveTo)

shivam chawla
- 57
- 6