Advertisement
vergepuppeter

EndlessRecyclerView

Aug 14th, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. import android.support.v7.widget.GridLayoutManager;
  2. import android.support.v7.widget.LinearLayoutManager;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.util.Log;
  5.  
  6. /**
  7.  * Created by Developer on 08/08/2016.
  8.  */
  9. public abstract class EndlessRecyclerOnScrollListener extends
  10.         RecyclerView.OnScrollListener {
  11.     public static String TAG = EndlessRecyclerOnScrollListener.class
  12.             .getSimpleName();
  13.  
  14.     private int previousTotal = 0;
  15.     private boolean loading = true;
  16.     private int visibleThreshold = 1;
  17.     int firstVisibleItem, visibleItemCount, totalItemCount;
  18.  
  19.     private int current_page = 1;
  20.     private GridLayoutManager mGridLayoutManager = null;
  21.     private LinearLayoutManager mLinearLayoutManager = null;
  22.  
  23.     public EndlessRecyclerOnScrollListener(RecyclerView.LayoutManager layoutManager) {
  24.         if(layoutManager instanceof GridLayoutManager)
  25.             this.mGridLayoutManager = (GridLayoutManager)layoutManager;
  26.         else
  27.             this.mLinearLayoutManager = (LinearLayoutManager)layoutManager;
  28.     }
  29.  
  30.     @Override
  31.     public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  32.         super.onScrolled(recyclerView, dx, dy);
  33.  
  34.         visibleItemCount = recyclerView.getChildCount();
  35.         if(null != mGridLayoutManager){
  36.             totalItemCount = mGridLayoutManager.getItemCount();
  37.             firstVisibleItem = mGridLayoutManager.findFirstVisibleItemPosition();
  38.         }
  39.         else if(null != mLinearLayoutManager){
  40.             totalItemCount = mLinearLayoutManager.getItemCount();
  41.             firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
  42.         }
  43.  
  44.         Log.i(TAG, visibleItemCount+", "+totalItemCount+", "+firstVisibleItem);
  45.  
  46.         if (loading) {
  47.             if (totalItemCount > previousTotal) {
  48.                 loading = false;
  49.                 previousTotal = totalItemCount;
  50.             }
  51.         }
  52.         if (!loading  && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
  53.             // End has been reached
  54.             Log.i("LoadMore invoked", totalItemCount - visibleItemCount + ", " +firstVisibleItem + visibleThreshold);
  55.             // Do something
  56.             current_page++;
  57.             onLoadMore(current_page);
  58.             loading = true;
  59.         }
  60.     }
  61.  
  62.     public abstract void onLoadMore(int current_page);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement