@Test public void snapOnScrollNextView() 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 scrollDistance = (getViewDimension(view) / 2) + 1; int scrollDist = mReverseScroll ? -scrollDistance : scrollDistance; mLayoutManager.expectIdleState(3); smoothScrollBy(scrollDist); mLayoutManager.waitForSnap(10); View viewAfterFling = findCenterView(mLayoutManager); assertNotSame("The view should have scrolled", view, viewAfterFling); int expectedPosition = mConfig.mItemCount / 2 + (mConfig.mReverseLayout ? (mReverseScroll ? 1 : -1) : (mReverseScroll ? -1 : 1)); assertEquals(expectedPosition, mLayoutManager.getPosition(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);  }