Forked from imran0101/EndlessRecyclerOnScrollListener.java
Created
December 30, 2016 12:51
-
-
Save mohanrajtrade/2e9b31b58e596321618c785b315cde1a to your computer and use it in GitHub Desktop.
RecyclerView position helper to get first and last visible positions
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
| /** | |
| * Custom Scroll listener for RecyclerView. | |
| * Based on implementation https://gist.github.com/ssinss/e06f12ef66c51252563e | |
| */ | |
| public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { | |
| public static String TAG = "EndlessScrollListener"; | |
| 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; | |
| RecyclerViewPositionHelper mRecyclerViewHelper; | |
| @Override | |
| public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
| super.onScrolled(recyclerView, dx, dy); | |
| mRecyclerViewHelper = RecyclerViewPositionHelper.createHelper(recyclerView); | |
| visibleItemCount = recyclerView.getChildCount(); | |
| totalItemCount = mRecyclerViewHelper.getItemCount(); | |
| firstVisibleItem = mRecyclerViewHelper.findFirstVisibleItemPosition(); | |
| if (loading) { | |
| if (totalItemCount > previousTotal) { | |
| loading = false; | |
| previousTotal = totalItemCount; | |
| } | |
| } | |
| if (!loading && (totalItemCount - visibleItemCount) | |
| <= (firstVisibleItem + visibleThreshold)) { | |
| // End has been reached | |
| // Do something | |
| current_page++; | |
| onLoadMore(current_page); | |
| loading = true; | |
| } | |
| } | |
| //Start loading | |
| public abstract void onLoadMore(int current_page); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment