Last active
October 23, 2018 07:15
-
-
Save Suleiman19/947898baaac4bd1aeeac588ba7e12e0e to your computer and use it in GitHub Desktop.
RecyclerView OnScrollListener for Pagination support
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 abstract class PaginationScrollListener extends RecyclerView.OnScrollListener { | |
LinearLayoutManager layoutManager; | |
public PaginationScrollListener(LinearLayoutManager layoutManager) { | |
this.layoutManager = layoutManager; | |
} | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
int visibleItemCount = layoutManager.getChildCount(); | |
int totalItemCount = layoutManager.getItemCount(); | |
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); | |
if (!isLoading() && !isLastPage()) { | |
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount | |
&& firstVisibleItemPosition >= 0 | |
&& totalItemCount >= getTotalPageCount()) { | |
loadMoreItems(); | |
} | |
} | |
} | |
protected abstract void loadMoreItems(); | |
public abstract int getTotalPageCount(); | |
public abstract boolean isLastPage(); | |
public abstract boolean isLoading(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment