Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
  2. // The minimum amount of items to have below your current scroll position
  3. // before loading more.
  4. private int visibleThreshold = 5;
  5.  
  6. // The current offset index of data you have loaded
  7. private int currentPage = 0;
  8.  
  9. // The total number of items in the dataset after the last load
  10. private int previousTotalItemCount = 0;
  11.  
  12. // True if we are still waiting for the last set of data to load.
  13. private boolean loading = true;
  14.  
  15. // Sets the starting page index
  16. private int startingPageIndex = 0;
  17.  
  18. private RecyclerView.LayoutManager mLayoutManager;
  19.  
  20. public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
  21. this.mLayoutManager = layoutManager;
  22. }
  23.  
  24. public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) {
  25. this.mLayoutManager = layoutManager;
  26. visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
  27. }
  28.  
  29. public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
  30. this.mLayoutManager = layoutManager;
  31. visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
  32. }
  33.  
  34. public int getLastVisibleItem(int[] lastVisibleItemPositions) {
  35. int maxSize = 0;
  36. for (int i = 0; i < lastVisibleItemPositions.length; i++) {
  37. if (i == 0) {
  38. maxSize = lastVisibleItemPositions[i];
  39. } else if (lastVisibleItemPositions[i] > maxSize) {
  40. maxSize = lastVisibleItemPositions[i];
  41. }
  42. }
  43. return maxSize;
  44. }
  45.  
  46. // This happens many times a second during a scroll, so be wary of the code you place here.
  47. // We are given a few useful parameters to help us work out if we need to load some more data,
  48. // but first we check if we are waiting for the previous load to finish.
  49. @Override
  50. public void onScrolled(RecyclerView view, int dx, int dy) {
  51. int lastVisibleItemPosition = 0;
  52. int totalItemCount = mLayoutManager.getItemCount();
  53.  
  54. if (mLayoutManager instanceof StaggeredGridLayoutManager) {
  55. int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
  56. // get maximum element within the list
  57. lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
  58. } else if (mLayoutManager instanceof GridLayoutManager) {
  59. lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
  60. } else if (mLayoutManager instanceof LinearLayoutManager) {
  61. lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
  62. }
  63.  
  64. // If it’s still loading, we check to see if the dataset count has
  65. // changed, if so we conclude it has finished loading and update the current page
  66. // number and total item count.
  67. if (loading && (totalItemCount > previousTotalItemCount)) {
  68. loading = false;
  69. previousTotalItemCount = totalItemCount;
  70. }
  71.  
  72. // If it isn’t currently loading, we check to see if we have breached
  73. // the visibleThreshold and need to reload more data.
  74. // If we do need to reload some more data, we execute onLoadMore to fetch the data.
  75. // threshold should reflect how many total columns there are too
  76. if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount
  77. && view.getAdapter().getItemCount() > visibleThreshold) {// This condition will useful when recyclerview has less than visibleThreshold items
  78. currentPage++;
  79. onLoadMore(currentPage, totalItemCount, view);
  80. loading = true;
  81. }
  82. }
  83.  
  84. // Call whenever performing new searches
  85. public void resetState() {
  86. this.currentPage = this.startingPageIndex;
  87. this.previousTotalItemCount = 0;
  88. this.loading = true;
  89. }
  90.  
  91. // Defines the process for actually loading more data based on page
  92. public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement