Skip to content

Instantly share code, notes, and snippets.

@snadjafi
Created April 24, 2014 20:53
Show Gist options
  • Select an option

  • Save snadjafi/11269151 to your computer and use it in GitHub Desktop.

Select an option

Save snadjafi/11269151 to your computer and use it in GitHub Desktop.
EndlessScrollListener
public class EndlessScrollListener implements AbsListView.OnScrollListener {
private int visibleThreshold = 3;
private int currentPage = 0;
private int currentTotalItems = 0;
private int firstItemPageIndex = 0;
private boolean loading = false;
private OnLoadMoreListener loadMoreListener;
public EndlessScrollListener(OnLoadMoreListener loadMoreListener) {
this.loadMoreListener = loadMoreListener;
}
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (totalItemCount < currentTotalItems) {
this.currentPage = this.firstItemPageIndex;
this.currentTotalItems = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
if (loading && (totalItemCount > currentTotalItems)) {
loading = false;
currentTotalItems = totalItemCount;
currentPage++;
}
if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
loadMoreListener.onLoadMore(currentPage + 1, totalItemCount);
loading = true;
}
}
public interface OnLoadMoreListener {
public void onLoadMore(int page, int totalItemsCount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment