Guest User

Untitled

a guest
Apr 29th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import android.support.v7.widget.LinearLayoutManager;
  2. import android.support.v7.widget.RecyclerView;
  3.  
  4. public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
  5. public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
  6.  
  7. private int previousTotal = 0; // The total number of items in the dataset after the last load
  8. private boolean loading = true; // True if we are still waiting for the last set of data to load.
  9. private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
  10. int firstVisibleItem, visibleItemCount, totalItemCount;
  11.  
  12. private int current_page = 1;
  13.  
  14. private LinearLayoutManager mLinearLayoutManager;
  15.  
  16. public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
  17. this.mLinearLayoutManager = linearLayoutManager;
  18. }
  19.  
  20. @Override
  21. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  22. super.onScrolled(recyclerView, dx, dy);
  23.  
  24. visibleItemCount = recyclerView.getChildCount();
  25. totalItemCount = mLinearLayoutManager.getItemCount();
  26. firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
  27.  
  28. if (loading) {
  29. if (totalItemCount > previousTotal) {
  30. loading = false;
  31. previousTotal = totalItemCount;
  32. }
  33. }
  34. if (!loading && (totalItemCount - visibleItemCount)
  35. <= (firstVisibleItem + visibleThreshold)) {
  36. // End has been reached
  37.  
  38. // Do something
  39. current_page++;
  40.  
  41. onLoadMore(current_page);
  42.  
  43. loading = true;
  44. }
  45. }
  46.  
  47. public abstract void onLoadMore(int current_page);
  48. }
Add Comment
Please, Sign In to add comment