package androidx.recyclerview.widget; import android.graphics.PointF; import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; public class LinearSnapHelper extends SnapHelper { private static final float INVALID_DISTANCE = 1.0f; @Nullable private OrientationHelper mHorizontalHelper; @Nullable private OrientationHelper mVerticalHelper; private float computeDistancePerChild(RecyclerView.LayoutManager layoutManager, OrientationHelper orientationHelper) { int max; int childCount = layoutManager.getChildCount(); if (childCount == 0) { return 1.0f; } View view = null; View view2 = null; int i = Integer.MAX_VALUE; int i2 = Integer.MIN_VALUE; for (int i3 = 0; i3 < childCount; i3++) { View childAt = layoutManager.getChildAt(i3); int position = layoutManager.getPosition(childAt); if (position != -1) { if (position < i) { view = childAt; i = position; } if (position > i2) { view2 = childAt; i2 = position; } } } if (view == null || view2 == null || (max = Math.max(orientationHelper.getDecoratedEnd(view), orientationHelper.getDecoratedEnd(view2)) - Math.min(orientationHelper.getDecoratedStart(view), orientationHelper.getDecoratedStart(view2))) == 0) { return 1.0f; } return (((float) max) * 1.0f) / ((float) ((i2 - i) + 1)); } private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View view, OrientationHelper orientationHelper) { return ((orientationHelper.getDecoratedMeasurement(view) / 2) + orientationHelper.getDecoratedStart(view)) - ((orientationHelper.getTotalSpace() / 2) + orientationHelper.getStartAfterPadding()); } private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager, OrientationHelper orientationHelper, int i, int i2) { int[] calculateScrollDistance = calculateScrollDistance(i, i2); float computeDistancePerChild = computeDistancePerChild(layoutManager, orientationHelper); if (computeDistancePerChild <= 0.0f) { return 0; } return Math.round(((float) (Math.abs(calculateScrollDistance[0]) > Math.abs(calculateScrollDistance[1]) ? calculateScrollDistance[0] : calculateScrollDistance[1])) / computeDistancePerChild); } @Nullable private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper orientationHelper) { int childCount = layoutManager.getChildCount(); View view = null; if (childCount == 0) { return null; } int totalSpace = (orientationHelper.getTotalSpace() / 2) + orientationHelper.getStartAfterPadding(); int i = Integer.MAX_VALUE; for (int i2 = 0; i2 < childCount; i2++) { View childAt = layoutManager.getChildAt(i2); int abs = Math.abs(((orientationHelper.getDecoratedMeasurement(childAt) / 2) + orientationHelper.getDecoratedStart(childAt)) - totalSpace); if (abs < i) { view = childAt; i = abs; } } return view; } @NonNull private OrientationHelper getHorizontalHelper(@NonNull RecyclerView.LayoutManager layoutManager) { OrientationHelper orientationHelper = this.mHorizontalHelper; if (orientationHelper == null || orientationHelper.mLayoutManager != layoutManager) { this.mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager); } return this.mHorizontalHelper; } @NonNull private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) { OrientationHelper orientationHelper = this.mVerticalHelper; if (orientationHelper == null || orientationHelper.mLayoutManager != layoutManager) { this.mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager); } return this.mVerticalHelper; } @Override // androidx.recyclerview.widget.SnapHelper public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View view) { int[] iArr = new int[2]; if (layoutManager.canScrollHorizontally()) { iArr[0] = distanceToCenter(layoutManager, view, getHorizontalHelper(layoutManager)); } else { iArr[0] = 0; } if (layoutManager.canScrollVertically()) { iArr[1] = distanceToCenter(layoutManager, view, getVerticalHelper(layoutManager)); } else { iArr[1] = 0; } return iArr; } @Override // androidx.recyclerview.widget.SnapHelper public View findSnapView(RecyclerView.LayoutManager layoutManager) { if (layoutManager.canScrollVertically()) { return findCenterView(layoutManager, getVerticalHelper(layoutManager)); } if (layoutManager.canScrollHorizontally()) { return findCenterView(layoutManager, getHorizontalHelper(layoutManager)); } return null; } @Override // androidx.recyclerview.widget.SnapHelper public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int i, int i2) { int itemCount; View findSnapView; int position; int i3; PointF computeScrollVectorForPosition; int i4; int i5; if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider) || (itemCount = layoutManager.getItemCount()) == 0 || (findSnapView = findSnapView(layoutManager)) == null || (position = layoutManager.getPosition(findSnapView)) == -1 || (computeScrollVectorForPosition = ((RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager).computeScrollVectorForPosition(itemCount - 1)) == null) { return -1; } int i6 = 0; if (layoutManager.canScrollHorizontally()) { i4 = estimateNextPositionDiffForFling(layoutManager, getHorizontalHelper(layoutManager), i, 0); if (computeScrollVectorForPosition.x < 0.0f) { i4 = -i4; } } else { i4 = 0; } if (layoutManager.canScrollVertically()) { i5 = estimateNextPositionDiffForFling(layoutManager, getVerticalHelper(layoutManager), 0, i2); if (computeScrollVectorForPosition.y < 0.0f) { i5 = -i5; } } else { i5 = 0; } if (layoutManager.canScrollVertically()) { i4 = i5; } if (i4 == 0) { return -1; } int i7 = position + i4; if (i7 >= 0) { i6 = i7; } return i6 >= itemCount ? i3 : i6; } }