0

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 :)

1 Answers1

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)