|
|
@@ -0,0 +1,85 @@
|
|
|
+/*
|
|
|
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
|
|
|
+ */
|
|
|
+package kr.co.zumo.app.lifeplus.view.screen.category;
|
|
|
+
|
|
|
+/**
|
|
|
+ * EndlessScrollListener
|
|
|
+ * <pre>
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author 민효동
|
|
|
+ * @version 1.0
|
|
|
+ * @history 민효동 [2019-03-26] [최초 작성]
|
|
|
+ * @since 2019-03-26
|
|
|
+ */
|
|
|
+
|
|
|
+import android.support.v7.widget.GridLayoutManager;
|
|
|
+import android.support.v7.widget.LinearLayoutManager;
|
|
|
+import android.support.v7.widget.RecyclerView;
|
|
|
+import android.support.v7.widget.StaggeredGridLayoutManager;
|
|
|
+
|
|
|
+public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
|
|
|
+ // The minimum amount of items to have below your current scroll position
|
|
|
+ // before loading more.
|
|
|
+ private int visibleThreshold = 1;
|
|
|
+
|
|
|
+ private RecyclerView.LayoutManager mLayoutManager;
|
|
|
+
|
|
|
+ public EndlessRecyclerViewScrollListener(RecyclerView.LayoutManager layoutManager) {
|
|
|
+ this.mLayoutManager = layoutManager;
|
|
|
+
|
|
|
+ if (mLayoutManager instanceof StaggeredGridLayoutManager) {
|
|
|
+ visibleThreshold = visibleThreshold * ((StaggeredGridLayoutManager) mLayoutManager).getSpanCount();
|
|
|
+ }
|
|
|
+ else if (mLayoutManager instanceof GridLayoutManager) {
|
|
|
+ visibleThreshold = visibleThreshold * ((GridLayoutManager) mLayoutManager).getSpanCount();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getLastVisibleItem(int[] lastVisibleItemPositions) {
|
|
|
+ int maxSize = 0;
|
|
|
+ for (int i = 0; i < lastVisibleItemPositions.length; i++) {
|
|
|
+ if (i == 0) {
|
|
|
+ maxSize = lastVisibleItemPositions[i];
|
|
|
+ }
|
|
|
+ else if (lastVisibleItemPositions[i] > maxSize) {
|
|
|
+ maxSize = lastVisibleItemPositions[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return maxSize;
|
|
|
+ }
|
|
|
+
|
|
|
+ // This happens many times a second during a scroll, so be wary of the code you place here.
|
|
|
+ // We are given a few useful parameters to help us work out if we need to load some more data,
|
|
|
+ // but first we check if we are waiting for the previous load to finish.
|
|
|
+ @Override
|
|
|
+ public void onScrolled(RecyclerView view, int dx, int dy) {
|
|
|
+ int lastVisibleItemPosition = 0;
|
|
|
+ int totalItemCount = mLayoutManager.getItemCount();
|
|
|
+
|
|
|
+ if (mLayoutManager instanceof StaggeredGridLayoutManager) {
|
|
|
+ int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
|
|
|
+ // get maximum element within the list
|
|
|
+ lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
|
|
|
+ }
|
|
|
+ else if (mLayoutManager instanceof GridLayoutManager) {
|
|
|
+ lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
|
|
|
+ }
|
|
|
+ else if (mLayoutManager instanceof LinearLayoutManager) {
|
|
|
+ lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
|
|
|
+ }
|
|
|
+
|
|
|
+ // If it isn’t currently loading, we check to see if we have breached
|
|
|
+ // the visibleThreshold and need to reload more data.
|
|
|
+ // If we do need to reload some more data, we execute onScrollEnd to fetch the data.
|
|
|
+ // threshold should reflect how many total columns there are too
|
|
|
+ if ((lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
|
|
|
+ onScrollEnd();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Defines the process for actually loading more data based on page
|
|
|
+ public abstract void onScrollEnd();
|
|
|
+
|
|
|
+}
|