Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Endless RecyclerView OnScrollListener
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;
public void reset(int previousTotal, boolean loading) {
this.previousTotal = previousTotal;
this.loading = loading;
this.pageIndex = 1;
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
loading = true;
public abstract void onLoadMore(int current_page);
import android.os.Bundle;
public class SampleActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
RecyclerView recyclerView = (RecyclerView) findViewById(;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
public void onLoadMore(int current_page) {
// do something...

This comment has been minimized.

Copy link
Owner Author

cyberrob commented Jan 28, 2016

Add reset method suggested from comments. Also reset the pageIndex to 1.


This comment has been minimized.

Copy link

pratamawijaya commented Jan 30, 2016

i can't find var pageIndex i think you missed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.