Procházet zdrojové kódy

[컨텐츠][Bug] 첫 페이지에서 연속 터치 시 이상하게 다음 페이지로 이동하는 버그 수정
- 헬퍼의 스크롤러에 버그가 있었다. 리싸이클러의 smoothScrollToPosition() 을 대신 이용

hyodong.min před 7 roky
rodič
revize
721744e848

+ 8 - 88
app/src/main/java/kr/co/zumo/app/lifeplus/view/screen/contents/CustomPagerSnapHelper.java

@@ -6,17 +6,11 @@ package kr.co.zumo.app.lifeplus.view.screen.contents;
 import android.graphics.PointF;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
-import android.support.v7.widget.LinearSmoothScroller;
 import android.support.v7.widget.OrientationHelper;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.SnapHelper;
-import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.View;
-import android.view.animation.DecelerateInterpolator;
-import android.widget.Scroller;
-
-import java.util.Arrays;
 
 /**
  * CustomPagerSnapHelper
@@ -29,33 +23,29 @@ import java.util.Arrays;
  * @since 2018. 12. 19.
  */
 public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
-  private static final int MAX_SCROLL_ON_FLING_DURATION = 100; // ms
-
   // Orientation helpers are lazily created per LayoutManager.
   @Nullable
   private OrientationHelper mVerticalHelper;
   @Nullable
   private OrientationHelper mHorizontalHelper;
+  private RecyclerView mRecyclerView;
 
   @Nullable
   public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) {
     int[] out = new int[2];
     if (layoutManager.canScrollHorizontally()) {
-      out[0] = distanceToCenter(layoutManager, targetView,
-        getHorizontalHelper(layoutManager));
+      out[0] = distanceToCenter(layoutManager, targetView, getHorizontalHelper(layoutManager));
     }
     else {
       out[0] = 0;
     }
 
     if (layoutManager.canScrollVertically()) {
-      out[1] = distanceToCenter(layoutManager, targetView,
-        getVerticalHelper(layoutManager));
+      out[1] = distanceToCenter(layoutManager, targetView, getVerticalHelper(layoutManager));
     }
     else {
       out[1] = 0;
     }
-    Log.w("APP# CustomPagerSnapHelper | calculateDistanceToFinalSnap", "|" + Arrays.toString(out));
     return out;
   }
 
@@ -114,35 +104,6 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
       : (forwardDirection ? centerPosition + 1 : centerPosition);
   }
 
-  protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
-    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
-      return null;
-    }
-    return new LinearSmoothScroller(mRecyclerView.getContext()) {
-      @Override
-      protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
-        int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(),
-          targetView);
-        final int dx = snapDistances[0];
-        final int dy = snapDistances[1];
-        final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
-        if (time > 0) {
-          action.update(dx, dy, time, mDecelerateInterpolator);
-        }
-      }
-
-      @Override
-      protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
-        return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
-      }
-
-      @Override
-      protected int calculateTimeForScrolling(int dx) {
-        return Math.min(MAX_SCROLL_ON_FLING_DURATION, super.calculateTimeForScrolling(dx));
-      }
-    };
-  }
-
   private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager,
                                @NonNull View targetView, OrientationHelper helper) {
     final int childCenter = helper.getDecoratedStart(targetView)
@@ -246,12 +207,6 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
     return mHorizontalHelper;
   }
 
-
-  static final float MILLISECONDS_PER_INCH = 100f;
-
-  RecyclerView mRecyclerView;
-  private Scroller mGravityScroller;
-
   // Handles the snap on scroll case.
   private final RecyclerView.OnScrollListener mScrollListener =
     new RecyclerView.OnScrollListener() {
@@ -285,8 +240,7 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
       return false;
     }
     int minFlingVelocity = mRecyclerView.getMinFlingVelocity();
-    return (Math.abs(velocityY) > minFlingVelocity || Math.abs(velocityX) > minFlingVelocity)
-      && snapFromFling(layoutManager, velocityX, velocityY);
+    return (Math.abs(velocityY) > minFlingVelocity || Math.abs(velocityX) > minFlingVelocity) && snapFromFling(layoutManager, velocityX, velocityY);
   }
 
   /**
@@ -311,8 +265,6 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
     mRecyclerView = recyclerView;
     if (mRecyclerView != null) {
       setupCallbacks();
-      mGravityScroller = new Scroller(mRecyclerView.getContext(),
-        new DecelerateInterpolator());
       snapToTargetExistingView();
     }
   }
@@ -336,23 +288,6 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
     mRecyclerView.setOnFlingListener(null);
   }
 
-  /**
-   * Calculated the estimated scroll distance in each direction given velocities on both axes.
-   *
-   * @param velocityX Fling velocity on the horizontal axis.
-   * @param velocityY Fling velocity on the vertical axis.
-   * @return array holding the calculated distances in x and y directions
-   * respectively.
-   */
-  public int[] calculateScrollDistance(int velocityX, int velocityY) {
-    int[] outDist = new int[2];
-    mGravityScroller.fling(0, 0, velocityX, velocityY,
-      Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
-    outDist[0] = mGravityScroller.getFinalX();
-    outDist[1] = mGravityScroller.getFinalY();
-    return outDist;
-  }
-
   /**
    * Helper method to facilitate for snapping triggered by a fling.
    *
@@ -362,24 +297,19 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
    * @param velocityY     Fling velocity on the vertical axis.
    * @return true if it is handled, false otherwise.
    */
-  private boolean snapFromFling(@NonNull RecyclerView.LayoutManager layoutManager, int velocityX,
-                                int velocityY) {
+  private boolean snapFromFling(@NonNull RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
     if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
       return false;
     }
 
-    RecyclerView.SmoothScroller smoothScroller = createScroller(layoutManager);
-    if (smoothScroller == null) {
-      return false;
-    }
+    Log.w("APP# CustomPagerSnapHelper | snapFromFling", "|" + "velocityX: " + velocityX);
 
     int targetPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY);
     if (targetPosition == RecyclerView.NO_POSITION) {
       return false;
     }
 
-    smoothScroller.setTargetPosition(targetPosition);
-    layoutManager.startSmoothScroll(smoothScroller);
+    mRecyclerView.smoothScrollToPosition(targetPosition);
     return true;
   }
 
@@ -389,6 +319,7 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
    * snapping was triggered by a scroll and when the fling is at its final stages.
    */
   void snapToTargetExistingView() {
+
     if (mRecyclerView == null) {
       return;
     }
@@ -406,17 +337,6 @@ public class CustomPagerSnapHelper extends RecyclerView.OnFlingListener {
     }
   }
 
-  /**
-   * Creates a scroller to be used in the snapping implementation.
-   *
-   * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached
-   *                      {@link RecyclerView}.
-   * @return a {@link RecyclerView.SmoothScroller} which will handle the scrolling.
-   */
-  @Nullable
-  protected RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager) {
-    return createSnapScroller(layoutManager);
-  }
 
 
 }