| 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.recyclerview.widget; |
| 18 | |
| 19 | import android.graphics.PointF; |
| 20 | import android.view.View; |
| 21 | |
| 22 | import androidx.annotation.NonNull; |
| 23 | import androidx.annotation.Nullable; |
| 24 | |
| 25 | /** |
| 26 | * Implementation of the {@link SnapHelper} supporting snapping in either vertical or horizontal |
| 27 | * orientation. |
| 28 | * <p> |
| 29 | * The implementation will snap the center of the target child view to the center of |
| 30 | * the attached {@link RecyclerView}. If you intend to change this behavior then override |
| 31 | * {@link SnapHelper#calculateDistanceToFinalSnap}. |
| 32 | */ |
| 33 | public class LinearSnapHelper extends SnapHelper { |
| 34 | |
| 35 | private static final float INVALID_DISTANCE = 1f; |
| 36 | |
| 37 | // Orientation helpers are lazily created per LayoutManager. |
| 38 | @Nullable |
| 39 | private OrientationHelper mVerticalHelper; |
| 40 | @Nullable |
| 41 | private OrientationHelper mHorizontalHelper; |
| 42 | |
| 43 | @Override |
| 44 | public int[] calculateDistanceToFinalSnap( |
| 45 | @NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { |
| 46 | int[] out = new int[2]; |
| 47 | if (layoutManager.canScrollHorizontally()) { |
| 48 | out[0] = distanceToCenter(layoutManager, targetView, |
| 49 | getHorizontalHelper(layoutManager)); |
| 50 | } else { |
| 51 | out[0] = 0; |
| 52 | } |
| 53 | |
| 54 | if (layoutManager.canScrollVertically()) { |
| 55 | out[1] = distanceToCenter(layoutManager, targetView, |
| 56 | getVerticalHelper(layoutManager)); |
| 57 | } else { |
| 58 | out[1] = 0; |
| 59 | } |
| 60 | return out; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, |
| 65 | int velocityY) { |
| 66 | if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { |
| 67 | return RecyclerView.NO_POSITION; |
| 68 | } |
| 69 | |
| 70 | final int itemCount = layoutManager.getItemCount(); |
| 71 | if (itemCount == 0) { |
| 72 | return RecyclerView.NO_POSITION; |
| 73 | } |
| 74 | |
| 75 | final View currentView = findSnapView(layoutManager); |
| 76 | if (currentView == null) { |
| 77 | return RecyclerView.NO_POSITION; |
| 78 | } |
| 79 | |
| 80 | final int currentPosition = layoutManager.getPosition(currentView); |
| 81 | if (currentPosition == RecyclerView.NO_POSITION) { |
| 82 | return RecyclerView.NO_POSITION; |
| 83 | } |
| 84 | |
| 85 | RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider = |
| 86 | (RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager; |
| 87 | // deltaJumps sign comes from the velocity which may not match the order of children in |
| 88 | // the LayoutManager. To overcome this, we ask for a vector from the LayoutManager to |
| 89 | // get the direction. |
| 90 | PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1); |
| 91 | if (vectorForEnd == null) { |
| 92 | // cannot get a vector for the given position. |
| 93 | return RecyclerView.NO_POSITION; |
| 94 | } |
| 95 | |
| 96 | int vDeltaJump, hDeltaJump; |
| 97 | if (layoutManager.canScrollHorizontally()) { |
| 98 | hDeltaJump = estimateNextPositionDiffForFling(layoutManager, |
| 99 | getHorizontalHelper(layoutManager), velocityX, 0); |
| 100 | if (vectorForEnd.x < 0) { |
| 101 | hDeltaJump = -hDeltaJump; |
| 102 | } |
| 103 | } else { |
| 104 | hDeltaJump = 0; |
| 105 | } |
| 106 | if (layoutManager.canScrollVertically()) { |
| 107 | vDeltaJump = estimateNextPositionDiffForFling(layoutManager, |
| 108 | getVerticalHelper(layoutManager), 0, velocityY); |
| 109 | if (vectorForEnd.y < 0) { |
| 110 | vDeltaJump = -vDeltaJump; |
| 111 | } |
| 112 | } else { |
| 113 | vDeltaJump = 0; |
| 114 | } |
| 115 | |
| 116 | int deltaJump = layoutManager.canScrollVertically() ? vDeltaJump : hDeltaJump; |
| 117 | if (deltaJump == 0) { |
| 118 | return RecyclerView.NO_POSITION; |
| 119 | } |
| 120 | |
| 121 | int targetPos = currentPosition + deltaJump; |
| 122 | if (targetPos < 0) { |
| 123 | targetPos = 0; |
| 124 | } |
| 125 | if (targetPos >= itemCount) { |
| 126 | targetPos = itemCount - 1; |
| 127 | } |
| 128 | return targetPos; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public View findSnapView(RecyclerView.LayoutManager layoutManager) { |
| 133 | if (layoutManager.canScrollVertically()) { |
| 134 | return findCenterView(layoutManager, getVerticalHelper(layoutManager)); |
| 135 | } else if (layoutManager.canScrollHorizontally()) { |
| 136 | return findCenterView(layoutManager, getHorizontalHelper(layoutManager)); |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager, |
| 142 | @NonNull View targetView, OrientationHelper helper) { |
| 143 | final int childCenter = helper.getDecoratedStart(targetView) |
| 144 | + (helper.getDecoratedMeasurement(targetView) / 2); |
| 145 | final int containerCenter; |
| 146 | if (layoutManager.getClipToPadding()) { |
| 147 | containerCenter = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; |
| 148 | } else { |
| 149 | containerCenter = helper.getEnd() / 2; |
| 150 | } |
| 151 | return childCenter - containerCenter; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Estimates a position to which SnapHelper will try to scroll to in response to a fling. |
| 156 | * |
| 157 | * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached |
| 158 | * {@link RecyclerView}. |
| 159 | * @param helper The {@link OrientationHelper} that is created from the LayoutManager. |
| 160 | * @param velocityX The velocity on the x axis. |
| 161 | * @param velocityY The velocity on the y axis. |
| 162 | * |
| 163 | * @return The diff between the target scroll position and the current position. |
| 164 | */ |
| 165 | private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager, |
| 166 | OrientationHelper helper, int velocityX, int velocityY) { |
| 167 | int[] distances = calculateScrollDistance(velocityX, velocityY); |
| 168 | float distancePerChild = computeDistancePerChild(layoutManager, helper); |
| 169 | if (distancePerChild <= 0) { |
| 170 | return 0; |
| 171 | } |
| 172 | int distance = |
| 173 | Math.abs(distances[0]) > Math.abs(distances[1]) ? distances[0] : distances[1]; |
| 174 | return (int) Math.round(distance / distancePerChild); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Return the child view that is currently closest to the center of this parent. |
| 179 | * |
| 180 | * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached |
| 181 | * {@link RecyclerView}. |
| 182 | * @param helper The relevant {@link OrientationHelper} for the attached {@link RecyclerView}. |
| 183 | * |
| 184 | * @return the child view that is currently closest to the center of this parent. |
| 185 | */ |
| 186 | @Nullable |
| 187 | private View findCenterView(RecyclerView.LayoutManager layoutManager, |
| 188 | OrientationHelper helper) { |
| 189 | int childCount = layoutManager.getChildCount(); |
| 190 | if (childCount == 0) { |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | View closestChild = null; |
| 195 | final int center; |
| 196 | if (layoutManager.getClipToPadding()) { |
| 197 | center = helper.getStartAfterPadding() + helper.getTotalSpace() / 2; |
| 198 | } else { |
| 199 | center = helper.getEnd() / 2; |
| 200 | } |
| 201 | int absClosest = Integer.MAX_VALUE; |
| 202 | |
| 203 | for (int i = 0; i < childCount; i++) { |
| 204 | final View child = layoutManager.getChildAt(i); |
| 205 | int childCenter = helper.getDecoratedStart(child) |
| 206 | + (helper.getDecoratedMeasurement(child) / 2); |
| 207 | int absDistance = Math.abs(childCenter - center); |
| 208 | |
| 209 | /** if child center is closer than previous closest, set it as closest **/ |
| 210 | if (absDistance < absClosest) { |
| 211 | absClosest = absDistance; |
| 212 | closestChild = child; |
| 213 | } |
| 214 | } |
| 215 | return closestChild; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Computes an average pixel value to pass a single child. |
| 220 | * <p> |
| 221 | * Returns a negative value if it cannot be calculated. |
| 222 | * |
| 223 | * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached |
| 224 | * {@link RecyclerView}. |
| 225 | * @param helper The relevant {@link OrientationHelper} for the attached |
| 226 | * {@link RecyclerView.LayoutManager}. |
| 227 | * |
| 228 | * @return A float value that is the average number of pixels needed to scroll by one view in |
| 229 | * the relevant direction. |
| 230 | */ |
| 231 | private float computeDistancePerChild(RecyclerView.LayoutManager layoutManager, |
| 232 | OrientationHelper helper) { |
| 233 | View minPosView = null; |
| 234 | View maxPosView = null; |
| 235 | int minPos = Integer.MAX_VALUE; |
| 236 | int maxPos = Integer.MIN_VALUE; |
| 237 | int childCount = layoutManager.getChildCount(); |
| 238 | if (childCount == 0) { |
| 239 | return INVALID_DISTANCE; |
| 240 | } |
| 241 | |
| 242 | for (int i = 0; i < childCount; i++) { |
| 243 | View child = layoutManager.getChildAt(i); |
| 244 | final int pos = layoutManager.getPosition(child); |
| 245 | if (pos == RecyclerView.NO_POSITION) { |
| 246 | continue; |
| 247 | } |
| 248 | if (pos < minPos) { |
| 249 | minPos = pos; |
| 250 | minPosView = child; |
| 251 | } |
| 252 | if (pos > maxPos) { |
| 253 | maxPos = pos; |
| 254 | maxPosView = child; |
| 255 | } |
| 256 | } |
| 257 | if (minPosView == null || maxPosView == null) { |
| 258 | return INVALID_DISTANCE; |
| 259 | } |
| 260 | int start = Math.min(helper.getDecoratedStart(minPosView), |
| 261 | helper.getDecoratedStart(maxPosView)); |
| 262 | int end = Math.max(helper.getDecoratedEnd(minPosView), |
| 263 | helper.getDecoratedEnd(maxPosView)); |
| 264 | int distance = end - start; |
| 265 | if (distance == 0) { |
| 266 | return INVALID_DISTANCE; |
| 267 | } |
| 268 | return 1f * distance / ((maxPos - minPos) + 1); |
| 269 | } |
| 270 | |
| 271 | @NonNull |
| 272 | private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) { |
| 273 | if (mVerticalHelper == null || mVerticalHelper.mLayoutManager != layoutManager) { |
| 274 | mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager); |
| 275 | } |
| 276 | return mVerticalHelper; |
| 277 | } |
| 278 | |
| 279 | @NonNull |
| 280 | private OrientationHelper getHorizontalHelper( |
| 281 | @NonNull RecyclerView.LayoutManager layoutManager) { |
| 282 | if (mHorizontalHelper == null || mHorizontalHelper.mLayoutManager != layoutManager) { |
| 283 | mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager); |
| 284 | } |
| 285 | return mHorizontalHelper; |
| 286 | } |
| 287 | } |
| 288 | |