Created
April 24, 2014 20:53
-
-
Save snadjafi/11269151 to your computer and use it in GitHub Desktop.
EndlessScrollListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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