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
It's pretty simple, simply set the RecyclerView 's height to wrap_content . That's right.
There is no way of making it infinite, but there is a way to make it look like infinite.
in your adapter override getCount()
to return something big like Integer.MAX_VALUE
:
@Override public int getCount() { return Integer.MAX_VALUE; }
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); }
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With