RecyclerView Circular

Let's say we have the following set of views: Views:

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

In the classic RecyclerView, View1 will be in the first position, and View (n) - in the last. Now I would like to make this circle, so after the last position we again scroll to the first position:

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

At first glance it seems simple, but I'm not sure how to perform this "reset" after View (n). Any advice would be very helpful. Thanks.

+5
source share
2 answers

Refer to this solution for ListView. You can write a RecyclerView.Adapter in which there are Integer.MAX_VALUE elements. The only difference is that RecyclerView no longer has the setSelectionFromTop method. Instead you should call

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

to first scroll through recyclerView to the middle.

+4
source

I did not do this myself, but I think this approach will work.

First you need to know which views are attached / visible. This can be done by a hierarchy change listener or a custom view, which will depend on your position. Anyway, when you get close to the end of the adapter, you just start to make your way; remove the elements from the other end of the adapter and add them to this end by calling notifyRangeInserted and notifyRangeRemoved, as you do. The deleted and pasted views must be outside the display area, so the animations will not be performed, although for GridLayoutManager you will need to work in multiple columns.

You will need to think about what to do when there is not enough content to fill out the recycler view. You probably also want to be at least a few elements before viewing the recycler, as it scrolls, you may need to add duplicates for this. I would rather move at least one screen of content forward at a time.

I assume the scroll indicator will disappear. A custom layout manager can give a good indicator.

0
source

Source: https://habr.com/ru/post/1213293/


All Articles