Pārlūkot izejas kodu

[공통][New] RecyclerView 페이지 계산 방식 변경
- 화면 중간을 기준으로 계산

hyodong.min 7 gadi atpakaļ
vecāks
revīzija
ec075ead61

+ 18 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/IChangedPageIndex.java

@@ -0,0 +1,18 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view;
+
+/**
+ * IChangedPageIndex
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 12. 17.]   [최초 작성]
+ * @since 2018. 12. 17.
+ */
+public interface IChangedPageIndex {
+  void onChangedPageIndex(int index);
+}

+ 72 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/view/IndexScrollListener.java

@@ -0,0 +1,72 @@
+/*
+ * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
+ */
+package kr.co.zumo.app.lifeplus.view;
+
+import android.support.v7.widget.LinearLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import android.view.View;
+
+/**
+ * IndexScrollListener
+ * <pre>
+ * </pre>
+ *
+ * @author 민효동
+ * @version 1.0
+ * @history 민효동   [2018. 12. 17.]   [최초 작성]
+ * @since 2018. 12. 17.
+ */
+public class IndexScrollListener extends RecyclerView.OnScrollListener {
+
+  private final LinearLayoutManager layoutManager;
+  private final IChangedPageIndex listener;
+  private int currentPageIndex = 0;
+
+  public IndexScrollListener(LinearLayoutManager layoutManager, IChangedPageIndex listener) {
+    this.layoutManager = layoutManager;
+    this.listener = listener;
+  }
+
+  @Override
+  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
+    int index = getCurrentIndex(layoutManager);
+
+    if (index > -1 && currentPageIndex != index) {
+      currentPageIndex = index;
+      listener.onChangedPageIndex(currentPageIndex);
+    }
+  }
+
+  /**
+   * LinearLayoutManager 를 통해 현재 index 를 가져온다.
+   * 레이아웃 영역 전체에 표시되는 아아템들에만 사용이 가능하다.
+   *
+   * @param layoutManager
+   * @return
+   */
+  public static int getCurrentIndex(LinearLayoutManager layoutManager) {
+    int half = layoutManager.getWidth() >> 1;
+    int firstIndex = layoutManager.findFirstVisibleItemPosition();
+    int lastIndex = layoutManager.findLastVisibleItemPosition();
+
+    View view = layoutManager.findViewByPosition(lastIndex);
+
+    if (null != view) {
+      int x = (int) view.getX();
+      int index;
+      if (half > x) {
+        // increase index
+        index = lastIndex;
+      }
+      else {
+        // decrease index
+        index = firstIndex;
+      }
+
+      return index;
+    }
+
+    return -1;
+  }
+}

+ 6 - 3
app/src/main/java/kr/co/zumo/app/lifeplus/view/custom/category/banner/MainBannerViewItemDotIndicator.java

@@ -8,9 +8,10 @@ import android.graphics.Rect;
 import android.support.annotation.ColorRes;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
-import android.util.Log;
 import android.view.View;
 
+import kr.co.zumo.app.lifeplus.view.IndexScrollListener;
+
 /**
  * MainBannerViewItemDotIndicator
  * <pre>
@@ -33,7 +34,7 @@ public class MainBannerViewItemDotIndicator extends RecyclerView.ItemDecoration
   /**
    * Height of the space the indicator takes up at the bottom of the view.
    */
-  private final int mIndicatorHeight = (int) (DP * 16 );
+  private final int mIndicatorHeight = (int) (DP * 16);
 
   /**
    * Indicator stroke width.
@@ -56,6 +57,8 @@ public class MainBannerViewItemDotIndicator extends RecyclerView.ItemDecoration
 
   private final Paint mPaint = new Paint();
 
+  private IndexScrollListener indexScrollListener;
+
   public MainBannerViewItemDotIndicator(Context context, @ColorRes int inActiveColor, @ColorRes int activeColor, int bottomOffest) {
     this.context = context;
     this.inActiveColor = inActiveColor;
@@ -88,7 +91,7 @@ public class MainBannerViewItemDotIndicator extends RecyclerView.ItemDecoration
 
     // find active page (which should be highlighted)
     LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
-    int activePosition = layoutManager.findFirstVisibleItemPosition();
+    int activePosition = IndexScrollListener.getCurrentIndex(layoutManager);
 //    Log.e("APP#  MainBannerViewItemDotIndicator | onDrawOver", "| activePosition: " + activePosition);
 
     if (activePosition == RecyclerView.NO_POSITION) {

+ 11 - 80
app/src/main/java/kr/co/zumo/app/lifeplus/view/custom/contents/CustomListicleImageView.java

@@ -22,6 +22,7 @@ import kr.co.zumo.app.R;
 import kr.co.zumo.app.lifeplus.bean.api.ContentsDetailImageBean;
 import kr.co.zumo.app.lifeplus.util.StringUtil;
 import kr.co.zumo.app.lifeplus.view.IEventListener;
+import kr.co.zumo.app.lifeplus.view.IndexScrollListener;
 
 /**
  * CustomListicleImageView
@@ -88,84 +89,16 @@ public class CustomListicleImageView extends ConstraintLayout {
     snapHelper.attachToRecyclerView(recyclerView);
   }
 
-  int oldX;
-  int oldY;
-
-  private static final String DIRECTION_NONE = "none";
-  private static final String DIRECTION_UP = "up";
-  private static final String DIRECTION_DOWN = "down";
-  private static final String DIRECTION_RIGHT = "right";
-  private static final String DIRECTION_LEFT = "left";
-
-  private String getDirection(MotionEvent e) {
-    String direction = DIRECTION_NONE;
-    int action = e.getAction();
-    switch (action) {
-      case MotionEvent.ACTION_DOWN:
-        oldX = (int) e.getX();
-        oldY = (int) e.getY();
-        break;
-      case MotionEvent.ACTION_MOVE:
-        int newX = (int) e.getX();
-        int newY = (int) e.getY();
-
-        int dx = oldX - newX;
-        int dy = oldY - newY;
-
-        // Use dx and dy to determine the direction of the move
-        if (Math.abs(dx) > Math.abs(dy)) {
-          if (dx > 0) {
-            direction = DIRECTION_RIGHT;
-          }
-          else {
-            direction = DIRECTION_LEFT;
-          }
-        }
-        else {
-          if (dy > 0) {
-            direction = DIRECTION_DOWN;
-          }
-          else {
-            direction = DIRECTION_UP;
-          }
-        }
-        break;
-      default:
-        // nothing
-        break;
-    }
-    return direction;
-  }
 
   private RecyclerView.OnItemTouchListener touchEventListener = new RecyclerView.OnItemTouchListener() {
     @Override
     public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
-      String direction = getDirection(e);
-//      Log.w("APP# CustomListicleImageView | onInterceptTouchEvent", "|" + "direction: " + direction);
-
       int action = e.getAction();
       switch (action) {
         case MotionEvent.ACTION_DOWN:
         case MotionEvent.ACTION_UP:
         case MotionEvent.ACTION_MOVE:
-
-          boolean intercept = true;
-          /**
-           * index 가 0 일 때 좌-> 우 드래그 시 false, index 가 끝일 때 좌<-우 드래그 시 false => 미사용
-           */
-
-//          if (currentIndex == 0) {
-//            if (direction.equals(DIRECTION_LEFT)) {
-//              intercept = false;
-//            }
-//          }
-//
-//          if (currentIndex == contentsLength - 1) {
-//            if (direction.equals(DIRECTION_RIGHT)) {
-//              intercept = false;
-//            }
-//          }
-          rv.getParent().requestDisallowInterceptTouchEvent(intercept);
+          rv.getParent().requestDisallowInterceptTouchEvent(true);
           break;
         default:
           break;
@@ -180,15 +113,7 @@ public class CustomListicleImageView extends ConstraintLayout {
     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {}
   };
 
-  private RecyclerView.OnScrollListener scrollEventListener = new RecyclerView.OnScrollListener() {
-    @Override
-    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
-      index = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
-      currentIndex = index % contentsLength;
-      textViewCurrentPage.setText(String.valueOf(currentIndex + 1));
-      textViewCurrentPage.setTypeface(null, Typeface.BOLD);
-    }
-  };
+  private RecyclerView.OnScrollListener scrollEventListener;
 
   public void init(Context context, int index, String title, String subTitle, List<ContentsDetailImageBean> contentsDetailImageBeans, IEventListener listener) {
 
@@ -242,10 +167,16 @@ public class CustomListicleImageView extends ConstraintLayout {
 
       }
     });
-    recyclerView.setAdapter(adapter);
-    recyclerView.addOnItemTouchListener(touchEventListener);
+    scrollEventListener = new IndexScrollListener(linearLayoutManager, idx -> {
+      CustomListicleImageView.this.index = idx;
+      currentIndex = idx % contentsLength;
+      textViewCurrentPage.setText(String.valueOf(currentIndex + 1));
+      textViewCurrentPage.setTypeface(null, Typeface.BOLD);
+    });
     recyclerView.addOnScrollListener(scrollEventListener);
+    recyclerView.addOnItemTouchListener(touchEventListener);
     recyclerView.scrollToPosition(contentsLength * 10000);
+    recyclerView.setAdapter(adapter);
     imageViewPrePage.setOnClickListener(view -> {onClickPrePage();});
     imageViewNextPage.setOnClickListener(view -> {onClickNextPage();});
   }

+ 30 - 34
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/contents/ContentsFragment.java

@@ -26,6 +26,7 @@ import kr.co.zumo.app.lifeplus.bean.api.SeriesItemBean;
 import kr.co.zumo.app.lifeplus.helper.ActionBarHelper;
 import kr.co.zumo.app.lifeplus.util.AppUtil;
 import kr.co.zumo.app.lifeplus.view.Event;
+import kr.co.zumo.app.lifeplus.view.IndexScrollListener;
 import kr.co.zumo.app.lifeplus.view.screen.FragmentBase;
 import kr.co.zumo.app.lifeplus.view.screen.main.TouchEventWithDirection;
 
@@ -46,6 +47,8 @@ public class ContentsFragment extends FragmentBase<ContentsPresenter> implements
   private ContentsAdapter adapter;
   private LinearLayoutManager layoutManager;
 
+  private RecyclerView.OnScrollListener scrollEventListener;
+
   @Override
   protected View onAfterCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     return inflater.inflate(R.layout.fragment_contents_detail, container, false);
@@ -57,7 +60,29 @@ public class ContentsFragment extends FragmentBase<ContentsPresenter> implements
     SnapHelper snapHelper = new PagerSnapHelper();
     snapHelper.attachToRecyclerView(recyclerViewContentsDetail);
 
-    recyclerViewContentsDetail.addOnScrollListener(scrollEventListener);
+    TouchEventWithDirection touchEventWithDirection = new TouchEventWithDirection(100);
+
+    recyclerViewContentsDetail.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
+      @Override
+      public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent motionEvent) {
+        if (touchEventWithDirection.getDirection(motionEvent).equals(TouchEventWithDirection.DIRECTION_RIGHT)) {
+          if (layoutManager.findFirstVisibleItemPosition() == adapter.getItemCount() - 1) {
+            presenter.onEvent(new Event.Builder(Event.LAST).build());
+          }
+        }
+        return false;
+      }
+
+      @Override
+      public void onTouchEvent(RecyclerView rv, MotionEvent motionEvent) {
+
+      }
+
+      @Override
+      public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
+
+      }
+    });
   }
 
   @Override
@@ -199,29 +224,11 @@ public class ContentsFragment extends FragmentBase<ContentsPresenter> implements
         startSmoothScroll(linearSmoothScroller);
       }
     };
-
-    TouchEventWithDirection touchEventWithDirection = new TouchEventWithDirection(100);
-    recyclerViewContentsDetail.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
-      @Override
-      public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent motionEvent) {
-        if (touchEventWithDirection.getDirection(motionEvent).equals(TouchEventWithDirection.DIRECTION_RIGHT)) {
-          if (layoutManager.findFirstVisibleItemPosition() == adapter.getItemCount() - 1) {
-            presenter.onEvent(new Event.Builder(Event.LAST).build());
-          }
-        }
-        return false;
-      }
-
-      @Override
-      public void onTouchEvent(RecyclerView rv, MotionEvent motionEvent) {
-
-      }
-
-      @Override
-      public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
-
-      }
+    scrollEventListener = new IndexScrollListener(layoutManager, index -> {
+      presenter.onChangedPageIndex(index);
     });
+    recyclerViewContentsDetail.addOnScrollListener(scrollEventListener);
+
     recyclerViewContentsDetail.setLayoutManager(layoutManager);
     recyclerViewContentsDetail.setAdapter(adapter);
   }
@@ -239,15 +246,4 @@ public class ContentsFragment extends FragmentBase<ContentsPresenter> implements
     return holder.dispatchBackPressed();
   }
 
-  private int currentPageIndex = 0;
-  private RecyclerView.OnScrollListener scrollEventListener = new RecyclerView.OnScrollListener() {
-    @Override
-    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
-      int index = layoutManager.findFirstVisibleItemPosition();
-      if (currentPageIndex != index) {
-        currentPageIndex = index;
-        presenter.onChangedPageIndex(currentPageIndex);
-      }
-    }
-  };
 }

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 1 - 1
app/src/sandbox/java/kr/co/zumo/app/lifeplus/network/api/LifeplusAPIService.java