Browse Source

[카테고리][New] 스크롤 끝으로가면(마지막 아이템이 보여지면) 다음 페이지 로드하도록 수정

hyodong.min 6 years ago
parent
commit
86bb80871f

+ 3 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/view/OverScrollDriver.java

@@ -51,11 +51,11 @@ public class OverScrollDriver {
 
   private void init() {
     listener = (appBarLayout, verticalOffset) -> {
-      Log.d("APP# OverScrollDriver | onOffsetChanged", "|" + " offset: " + verticalOffset);
-      Log.d("APP# OverScrollDriver | onOffsetChanged", "|" + " range: " + appBarLayout.getTotalScrollRange());
+//      Log.d("APP# OverScrollDriver | onOffsetChanged", "|" + " offset: " + verticalOffset);
+//      Log.d("APP# OverScrollDriver | onOffsetChanged", "|" + " range: " + appBarLayout.getTotalScrollRange());
 
       if (collapsedStatus == NONE) {
-        Log.d("APP# OverScrollDriver | init", "|" + " collapsedStatus == NONE");
+//        Log.d("APP# OverScrollDriver | init", "|" + " collapsedStatus == NONE");
         return;
       }
 

+ 7 - 7
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/category/CategoryMainFragment.java

@@ -8,7 +8,6 @@ import android.support.design.widget.CoordinatorLayout;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.Toolbar;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -33,7 +32,6 @@ import kr.co.zumo.app.lifeplus.view.custom.category.series.IMainSeriesContract;
 import kr.co.zumo.app.lifeplus.view.custom.category.series.MainSeriesPresenter;
 import kr.co.zumo.app.lifeplus.view.custom.category.series.MainSeriesView;
 import kr.co.zumo.app.lifeplus.view.screen.FragmentBase;
-import me.everything.android.ui.overscroll.IOverScrollState;
 
 /**
  * CategoryMainFragment
@@ -112,14 +110,16 @@ public abstract class CategoryMainFragment<P extends CategoryMainPresenter> exte
       public void onNothingSelected(AdapterView<?> parent) {}
     });
 
-    overScrollDriver = new OverScrollDriver(recyclerView, appBarLayout, (decor, oldState, newState) -> {
-      Log.d("APP# CategoryMainFragment | onOverScrollStateChange", "|" + "bounce back --> " + oldState);
-      if (newState == IOverScrollState.STATE_BOUNCE_BACK && oldState == IOverScrollState.STATE_DRAG_END_SIDE) {
-        // View is starting to bounce back from the *right-end*.
+    overScrollDriver = new OverScrollDriver(recyclerView, appBarLayout, (decor, oldState, newState) -> {});
+    overScrollDriver.setCollapsedStatusNone();
+
+    recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(recyclerView.getLayoutManager()) {
+      @Override
+      public void onScrollEnd() {
         presenter.onOverScrollEnd();
       }
     });
-    overScrollDriver.setCollapsedStatusNone();
+
   }
 
   @Override

+ 37 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/category/CategoryMainModel.java

@@ -202,15 +202,16 @@ public abstract class CategoryMainModel extends Model {
     return bannerBeans;
   }
 
-  private int currentPage = 1;
+  private int currentPage = 0;
   private int countPerPage = 20;
   private int lastListCount = countPerPage;
+  private boolean isAppending = false;
 
   /**
    * 페이징 리셋
    */
   public void resetPaging() {
-    currentPage = 1;
+    currentPage = 0;
     lastListCount = countPerPage;
   }
 
@@ -226,10 +227,32 @@ public abstract class CategoryMainModel extends Model {
   /**
    * 페이지 증가
    */
-  public void increasePage() {
+  private void increasePage() {
     ++currentPage;
   }
 
+  private void decreasePage() {
+    --currentPage;
+  }
+
+  /**
+   * 붙임 작업 중 설정
+   *
+   * @param bool
+   */
+  public void setAppending(boolean bool) {
+    isAppending = bool;
+  }
+
+  /**
+   * 붙임 작업 중 확인
+   *
+   * @return
+   */
+  public boolean isAppending() {
+    return isAppending;
+  }
+
   /**
    * 추가된 콘텐츠의 시작 인덱스 반환
    *
@@ -244,6 +267,7 @@ public abstract class CategoryMainModel extends Model {
    * - 첫 페이지부터 로드함
    */
   public void loadContents() {
+    contentsBeans = new ArrayList<>();
     resetPaging();
     appendContents(event -> {
       if (event.getEventId() == Event.SUCCESS) {
@@ -261,7 +285,16 @@ public abstract class CategoryMainModel extends Model {
    * @param listener
    */
   public void appendContents(IEventListener listener) {
+    setAppending(true);
+    increasePage();
+
     disposableContents = new APICategoryContentsLoadModule(currentOrder).call(new CategoryContentsRequestBean(getCategoryNo(), String.valueOf(currentPage), String.valueOf(countPerPage)), new APIModuleListener<CategoryContentsResultBean>(waiterCaller) {
+      @Override
+      public void onApiEnd() {
+        super.onApiEnd();
+        setAppending(false);
+      }
+
       @Override
       public void onApiSuccess(CategoryContentsResultBean resultBean) {
         List<CategoryContentsBean> list = resultBean.getData();
@@ -281,6 +314,7 @@ public abstract class CategoryMainModel extends Model {
 
       @Override
       public void onApiError(String errorMessage, APIError error) {
+        decreasePage();
         listener.onEvent(new Event.Builder(Event.ERROR).integer(Event.LOADED_CONTENTS).string(errorMessage).build());
       }
     });

+ 1 - 2
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/category/CategoryMainPresenter.java

@@ -369,8 +369,7 @@ public abstract class CategoryMainPresenter<M extends CategoryMainModel, V exten
    * 하단 스크롤 끝
    */
   public void onOverScrollEnd() {
-    if (model.hasNextPage()) {
-      model.increasePage();
+    if (model.hasNextPage() && model.isAppending() == false) {
       model.appendContents(event -> {
         if (event.getEventId() == Event.SUCCESS) {
           view.appendContents(model.getContents(), model.getAppendIndex());

+ 85 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/category/EndlessRecyclerViewScrollListener.java

@@ -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();
+
+}