I want to implement an endless RecyclerView but so far all the implementations and tutorials I have come across describe loading data off a server then loading it or dynamically adding elements using a loop.
In my case, I already have a large ArrayList containing the data I want to display in the RecyclerView.
What I need is to say, display the first 10 items of the array, and on loading more, display the next batch of 10.
This could achieve your goal.
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LinearLayoutManager mLinearLayoutManager;
public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
And sample activity
public class SampleActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// do something...
}
});
}
}
Edit: See here: Endless Scrolling with AdapterViews
You can use this Endless Adapter
- It is generic, you give it (You model, Recycler.ViewHolder)
- You can set the adapter either show loading or not.
- Have Listeners to check when loading finished, and when loading starts.
You can check also the sample for more info
Try using:
recyclerView.addOnScrollListener (recyclerViewOnScrollListener);
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener () {
private final String LOG_TAG = "PAGINATION_";
boolean isIdle;
@Override
public void onScrollStateChanged (RecyclerView recyclerView, int newState) {
try {
super.onScrollStateChanged (recyclerView, newState);
isIdle = (newState == RecyclerView.SCROLL_STATE_IDLE);
}
catch (Exception e) {
e.printStackTrace ();
}
}
@Override
public void onScrolled (RecyclerView recyclerView, int dx, int dy) {
firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition ();
super.onScrolled (recyclerView, dx, dy);
if (!isIdle dy > 0) {
int totalItems = yourList.size()();
totalItemCount = layoutManager.getItemCount ();
visibleItemCount = layoutManager.getChildCount ();
Log.i (LOG_TAG, "Max: " + totalItems + ", Total: " + totalItemCount + ", Previous Total: " + previousTotal + ", Visible: " + visibleItemCount + ", First: " + firstVisibleItemPosition);
if (isLoading) {
if (totalItemCount != previousTotal) {
isLoading = false;
previousTotal = totalItemCount;
}
}
isLastPage = (totalItemCount >= totalItems);
Log.i (LOG_TAG, "DETAILS: currentPage: " + currentPage + ", isLoading: " + isLoading + ", isLast: " + isLastPage);
if (!isLoading && !isLastPage) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0 && totalItems >= (currentPage * PAGE_LIMIT) && totalItems > totalItemCount) {
if (totalItems > (currentPage * PAGE_LIMIT)) {
currentPage = (totalItemCount / PAGE_LIMIT) + 1;
isLoading = true;
//load more data using currentPage and PAGE_LIMIT
}
}
}
}
previousTotal = totalItemCount;
}
catch (Exception e) {
e.printStackTrace ();
}
}
}
}
};
try using this class it help me alot :
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
RecyclerView.LayoutManager mLayoutManager;
public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
}
public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
this.mLayoutManager = layoutManager;
visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
}
public int getLastVisibleItem(int[] lastVisibleItemPositions) {
int maxSize = 0;
for (int i = 0; i < lastVisibleItemPositions.length; i++) {
if (i == 0) {
maxSize = lastVisibleItemPositions[i];
}
else if (lastVisibleItemPositions[i] > maxSize) {
maxSize = lastVisibleItemPositions[i];
}
}
return maxSize;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
int lastVisibleItemPosition = 0;
int totalItemCount = mLayoutManager.getItemCount();
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
// get maximum element within the list
lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
} else if (mLayoutManager instanceof GridLayoutManager) {
lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
} else if (mLayoutManager instanceof LinearLayoutManager) {
lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
}
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) {
this.loading = true;
}
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
// threshold should reflect how many total columns there are too
if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount, view);
loading = true;
}
}
// Call this method whenever performing new searches
public void resetState() {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = 0;
this.loading = true;
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
}
reference :
- https://gist.github.com/nesquena/d09dc68ff07e845cc622
- https://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView