@Test public void snapOnFlingSameView() throws Throwable { final Config config = (Config) mConfig.clone(); setupByConfig(config, true, new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT), new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); setupSnapHelper(); View view = findCenterView(mLayoutManager); assertCenterAligned(view); int velocity = (int) (1.000001 * mRecyclerView.getMinFlingVelocity()); int velocityDir = mReverseScroll ? -velocity : velocity; mLayoutManager.expectIdleState(2); mActivityRule.runOnUiThread(new Runnable() { @Override public void run() { mRecyclerView.scrollBy(mReverseScroll ? -1 : 1, mReverseScroll ? -1 : 1); } }); waitForIdleScroll(mRecyclerView); assertTrue(fling(velocityDir, velocityDir)); waitForIdleScroll(mRecyclerView); mLayoutManager.waitForSnap(100); View viewAfterFling = findCenterView(mLayoutManager); assertSame("The view should NOT have scrolled", view, viewAfterFling); assertCenterAligned(viewAfterFling); }
@Nullable private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) { int childCount = layoutManager.getChildCount(); if (childCount == 0) { return null; } View closestChild = null; final int center; if (layoutManager.getClipToPadding()) { center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; } else { center = helper.getEnd() / 2; } int absClosest = Integer.MAX_VALUE; for (int i = 0; i < childCount; i++) { final View child = layoutManager.getChildAt(i); int childCenter = helper.getDecoratedStart(child) + (helper.getDecoratedMeasurement(child) / 2); int absDistance = Math.abs(childCenter - center); if (absDistance < absClosest) { absClosest = absDistance; closestChild = child; } } return closestChild; }
PagerSnapHelper extends SnapHelper { @Nullable private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) { int childCount = layoutManager.getChildCount(); if (childCount == 0) { return null; } View closestChild = null; final int center; if (layoutManager.getClipToPadding()) { center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; } else { center = helper.getEnd() / 2; } int absClosest = Integer.MAX_VALUE; for (int i = 0; i < childCount; i++) { final View child = layoutManager.getChildAt(i); int childCenter = helper.getDecoratedStart(child) + (helper.getDecoratedMeasurement(child) / 2); int absDistance = Math.abs(childCenter - center); if (absDistance < absClosest) { absClosest = absDistance; closestChild = child; } } return closestChild; } }
PagerSnapHelper extends SnapHelper { @Nullable private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) { int childCount = layoutManager.getChildCount(); if (childCount == 0) { return null; } View closestChild = null; final int center; if (layoutManager.getClipToPadding()) { center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; } else { center = helper.getEnd() / 2; } int absClosest = Integer.MAX_VALUE; for (int i = 0; i < childCount; i++) { final View child = layoutManager.getChildAt(i); int childCenter = helper.getDecoratedStart(child) + (helper.getDecoratedMeasurement(child) / 2); int absDistance = Math.abs(childCenter - center); if (absDistance < absClosest) { absClosest = absDistance; closestChild = child; } } return closestChild; }  }
PagerSnapHelper extends SnapHelper { @Nullable private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) { int childCount = layoutManager.getChildCount(); if (childCount == 0) { return null; } View closestChild = null; final int center; if (layoutManager.getClipToPadding()) { center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; } else { center = helper.getEnd() / 2; } int absClosest = Integer.MAX_VALUE; for (int i = 0; i < childCount; i++) { final View child = layoutManager.getChildAt(i); int childCenter = helper.getDecoratedStart(child) + (helper.getDecoratedMeasurement(child) / 2); int absDistance = Math.abs(childCenter - center); if (absDistance < absClosest) { absClosest = absDistance; closestChild = child; } } return closestChild; }  @Nullable @Override int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
            @NonNull View targetView); @Nullable @Override View findSnapView(RecyclerView.LayoutManager layoutManager); @Override int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX,
            int velocityY); }
PagerSnapHelper extends SnapHelper { @Nullable private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) { int childCount = layoutManager.getChildCount(); if (childCount == 0) { return null; } View closestChild = null; final int center; if (layoutManager.getClipToPadding()) { center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; } else { center = helper.getEnd() / 2; } int absClosest = Integer.MAX_VALUE; for (int i = 0; i < childCount; i++) { final View child = layoutManager.getChildAt(i); int childCenter = helper.getDecoratedStart(child) + (helper.getDecoratedMeasurement(child) / 2); int absDistance = Math.abs(childCenter - center); if (absDistance < absClosest) { absClosest = absDistance; closestChild = child; } } return closestChild; }  @Nullable @Override int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager,
            @NonNull View targetView); @Nullable @Override View findSnapView(RecyclerView.LayoutManager layoutManager); @Override int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX,
            int velocityY);  }