Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / components / recycler / EndlessRecyclerOnScrollListener.java
diff --git a/Android/app/src/main/java/com/moiseum/wolnelektury/components/recycler/EndlessRecyclerOnScrollListener.java b/Android/app/src/main/java/com/moiseum/wolnelektury/components/recycler/EndlessRecyclerOnScrollListener.java
new file mode 100644 (file)
index 0000000..073dff1
--- /dev/null
@@ -0,0 +1,50 @@
+package com.moiseum.wolnelektury.components.recycler;
+
+/**
+ * Created by Piotr Ostrowski on 28.11.2017.
+ */
+
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+
+public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
+
+       public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
+
+       /**
+        * The total number of items in the dataset after the last load
+        */
+       private int mPreviousTotal = 0;
+       /**
+        * True if we are still waiting for the last set of data to load.
+        */
+       private boolean mLoading = true;
+
+       @Override
+       public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
+               super.onScrolled(recyclerView, dx, dy);
+
+               int visibleItemCount = recyclerView.getChildCount();
+               int totalItemCount = recyclerView.getLayoutManager().getItemCount();
+               int firstVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
+
+               if (mLoading) {
+                       if (totalItemCount > mPreviousTotal) {
+                               mLoading = false;
+                               mPreviousTotal = totalItemCount;
+                       }
+               }
+               int visibleThreshold = 5;
+               if (!mLoading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
+                       // End has been reached
+                       onLoadMore();
+                       mLoading = true;
+               }
+       }
+
+       public void reset() {
+               mPreviousTotal = 0;
+       }
+
+       public abstract void onLoadMore();
+}
\ No newline at end of file