Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a circular (endless) RecyclerView?

I am trying to make my RecyclerView loop back to the start of my list.

I have searched all over the internet and have managed to detect when I have reached the end of my list, however I am unsure where to proceed from here.

This is what I am currently using to detect the end of the list (found here):

 @Override     public void onScrolled(RecyclerView recyclerView, int dx, int dy) {          visibleItemCount = mLayoutManager.getChildCount();         totalItemCount = mLayoutManager.getItemCount();         pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();          if (loading) {             if ( (visibleItemCount+pastVisiblesItems) >= totalItemCount) {                 loading = false;                 Log.v("...", ""+visibleItemCount);             }         }  } 

When scrolled to the end, I would like to views to be visible while the displaying data from the top of the list or when scrolled to the top of the list I would display data from the bottom of the list.

For example:

View1 View2 View3 View4 View5

View5 View1 View2 View3 View4

like image 778
ltsai Avatar asked Jul 06 '15 18:07

ltsai


People also ask

How do you load all items in RecyclerView without scrolling?

It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.


1 Answers

There is no way of making it infinite, but there is a way to make it look like infinite.

  1. in your adapter override getCount() to return something big like Integer.MAX_VALUE:

    @Override public int getCount() {     return Integer.MAX_VALUE; } 
  2. in getItem() and getView() modulo divide (%) position by real item number:

    @Override public Fragment getItem(int position) {     int positionInList = position % fragmentList.size();     return fragmentList.get(positionInList); } 
  3. at the end, set current item to something in the middle (or else, it would be endless only in downward direction).

    // scroll to middle item recyclerView.getLayoutManager().scrollToPosition(Integer.MAX_VALUE / 2); 
like image 172
Sebastian Pakieła Avatar answered Oct 10 '22 23:10

Sebastian Pakieła