Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gugacavalieri/fe65cf9d33395caa469d to your computer and use it in GitHub Desktop.
Save gugacavalieri/fe65cf9d33395caa469d to your computer and use it in GitHub Desktop.
Endless RecyclerView OnScrollListener
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
/**
* Class for extending Recycler view functionality
* Allows for fetching more data when user reaches end of view!
*/
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 pageSize = 25;
private int current_page = 0;
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
if(totalItemCount >= pageSize) {
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
}
/*
* We can set the page size according to our api configuration (:
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* reset method to be called when a new set of results is passed to adapter!
*/
public void reset() {
current_page = 0;
}
public abstract void onLoadMore(int current_page);
}
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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);
/* using addOnScrollListener on our recycler view since 'setOnScrollListener' is deprecated */
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
// do something...
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment