Skip to content

Instantly share code, notes, and snippets.

@plsankar
Created July 7, 2020 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plsankar/65fd0be53725d7f44b7f301a546a9474 to your computer and use it in GitHub Desktop.
Save plsankar/65fd0be53725d7f44b7f301a546a9474 to your computer and use it in GitHub Desktop.
public abstract class LoadMoreListener extends RecyclerView.OnScrollListener {
private static final int ITEMS_PER_PAGE = 10;
private boolean loading = false;
private int page = 1;
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(1)
&& !loading
&& recyclerView.getAdapter() != null
&& recyclerView.getAdapter().getItemCount() >= ITEMS_PER_PAGE) {
page = page + 1;
loading = true;
onLoadMore(page);
}
}
public void setLoading(boolean loading) {
this.loading = loading;
}
public void reset() {
loading = false;
page = 1;
}
public abstract void onLoadMore(int page);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment