user4386126
user4386126

Reputation: 1245

Circular RecyclerView

Let's say we the following set of item Views:

View1 -> View2 -> View3 -> ... -> View(n-1) -> View(n)

On classic RecyclerView, View1 would be on first position and View(n) on last. Now I would like to make that circular so after last position we scroll again to first position:

View1 -> View2 -> View3 -> ... -> View(n-1) -> View(n) -> View1 -> View2 -> ...

This at first look seems simple to make but I am not sure how to accomplish that "reset" after View(n). Any advice would be really helpful. Thank you.

Upvotes: 5

Views: 4563

Answers (2)

misaka-10032
misaka-10032

Reputation: 179

Please refer to this solution for ListView. You may write a RecyclerView.Adapter that has Integer.MAX_VALUE items. The only difference is that RecyclerView no longer has setSelectionFromTop method. Instead you should call

recyclerView.getLayoutManager().scrollToPosition(recyclerAdapter.MIDDLE);

in order to scroll the recyclerView to the middle at first.

Upvotes: 5

Tonic Artos
Tonic Artos

Reputation: 1568

I haven't done this myself, but I think this approach would work.

First you need to know which views are attached/visible. A hierarchy change listener could do this, or a custom view that shims your item views. Anyway, when you are near to an end of the adapter you simply start laying track in of you; remove items from the other end of the adapter and add them to the this end, calling notifyRangeInserted and notifyRangeRemoved as you do this. The removed and inserted views should be outside of the displayed area so no animations will take place, though for GridLayoutManager you will need to work in multiples of the number of columns.

You will have to think about what to do when there isn't enough content to fill the recycler view. You'll also probably want to be at least a few items ahead of the recycler view as it scrolls, you might have to add duplicates to do this. I would prefer to move at least one screen of content ahead at a time.

I imagine the scroll indicator will bug out. A custom layout manager could give a pretty good indicator.

Upvotes: 0

Related Questions