Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Last active October 2, 2017 05:22
Show Gist options
  • Save rezaiyan/534347835f76fe4ee6387f36b0ba1411 to your computer and use it in GitHub Desktop.
Save rezaiyan/534347835f76fe4ee6387f36b0ba1411 to your computer and use it in GitHub Desktop.
Endless recyclerview with two methodlogy
//Old endless recyclerview methodology
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = gridLayoutManager.getItemCount();
firstVisibleItem = gridLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
Log.i(TAG, "onScrolled: " + page);
inputQuery.put(PAGE,String.valueOf(page));
presenter.getProductByFilter(inputQuery);
loading = true;
}
}
});
//New endless recyclerview methodology
//1- in adapter
public interface EndEvent{
void reach();
}
EndEvent event;
public void setEndEvent(EndEvent event){
this.event=event;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
boolean lastPositionReached = position == productResults.size() - 1;
if (lastPositionReached) {
event.reach();//reach to end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment