studersi studersi - 8 months ago 128
Android Question

How to cycle through items in Android RecyclerView?

How can you cycle through the items in an Android RecyclerView?

I would like to have a RecyclerView that scrolls horizontally. When the end is reached to the right, it simply continues scrolling by restarting the list of items. the same to the left.

For example:

List of items: 0,1,2,3,4,5,6,7,8,9

Starting view shows: 0,1,2,3,4,5,6,7

After scrolling 5 items to the right: 5,6,7,8,9,0,1,2

After scrolling 2 items to the left: 8,9,0,1,2,3,4,5


another idea would be, getItemCount() of your adapter return Integer.MAX_VALUE and then in order to get your item you should do itemsList.get(position % list.size)
this way when the scrolling goes beyond your actual list size it starts from 0 again and show the first element of the list after the last one

one more thing to do could be calling scrollToPosition(int x) on the LayoutManager where x % yourList.size() == 0 and x being somwhere close to Integer.MAX_VALUE / 2 in this way the scroll would look like infinite (~1 billion positions in every direction from the starting point)