PagedSnapHelper.java revision b1df1da479733666c3c877ff3fdc602f9d3f271a
1b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen/* 2b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Copyright (C) 2017 The Android Open Source Project 3b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 4b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Licensed under the Apache License, Version 2.0 (the "License"); 5b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * you may not use this file except in compliance with the License. 6b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * You may obtain a copy of the License at 7b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 8b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * http://www.apache.org/licenses/LICENSE-2.0 9b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 10b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Unless required by applicable law or agreed to in writing, software 11b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * distributed under the License is distributed on an "AS IS" BASIS, 12b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * See the License for the specific language governing permissions and 14b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * limitations under the License. 15b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 16b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 17b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenpackage androidx.car.widget; 18b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 19f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chenimport android.content.Context; 20b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenimport android.support.annotation.NonNull; 21b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenimport android.support.annotation.Nullable; 22b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenimport android.support.v7.widget.LinearSnapHelper; 23b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenimport android.support.v7.widget.OrientationHelper; 24b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenimport android.support.v7.widget.RecyclerView; 25b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenimport android.view.View; 26b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 27b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen/** 28b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Extension of a {@link LinearSnapHelper} that will snap to the start of the target child view to 29b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * the start of the attached {@link RecyclerView}. The start of the view is defined as the top 30b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * if the RecyclerView is scrolling vertically; it is defined as the left (or right if RTL) if the 31b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * RecyclerView is scrolling horizontally. 32b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * 33b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * <p>Snapping may be disabled for views whose height is greater than that of the 34b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * {@code RecyclerView} that contains them. In this case, the view will only be snapped to when it 35b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * is first encountered. Otherwise, the user will be allowed to scroll freely through that view 36b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * when it appears in the list. The snapping behavior will resume when the large view is scrolled 37b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * off-screen. 38b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 39b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chenpublic class PagedSnapHelper extends LinearSnapHelper { 40b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** 41b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * The percentage of a View that needs to be completely visible for it to be a viable snap 42b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * target. 43b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 44b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen private static final float VIEW_VISIBLE_THRESHOLD = 0.5f; 45b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 46f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen private final PagedSmoothScroller mSmoothScroller; 47b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen private RecyclerView mRecyclerView; 48b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 49b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // Orientation helpers are lazily created per LayoutManager. 50b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen @Nullable private OrientationHelper mVerticalHelper; 51b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen @Nullable private OrientationHelper mHorizontalHelper; 52b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 53f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen public PagedSnapHelper(Context context) { 54f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen mSmoothScroller = new PagedSmoothScroller(context); 55f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen } 56f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen 57b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @Override 58b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, 59b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @NonNull View targetView) { 60b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int[] out = new int[2]; 61b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 62b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen out[0] = layoutManager.canScrollHorizontally() 63b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen ? getHorizontalHelper(layoutManager).getDecoratedStart(targetView) 64b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen : 0; 65b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 66b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen out[1] = layoutManager.canScrollVertically() 67b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen ? getVerticalHelper(layoutManager).getDecoratedStart(targetView) 68b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen : 0; 69b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 70b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return out; 71b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 72b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 73b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** 74b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Finds the view to snap to. The view to snap to is the child of the LayoutManager that is 75b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * closest to the start of the RecyclerView. The "start" depends on if the LayoutManager 76b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * is scrolling horizontally or vertically. If it is horizontally scrolling, then the 77b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * start is the view on the left (right if RTL). Otherwise, it is the top-most view. 78b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 79b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param layoutManager The current {@link RecyclerView.LayoutManager} for the attached 80b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * RecyclerView. 81b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @return The View closest to the start of the RecyclerView. 82b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 83b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @Override 84b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @Nullable 85b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen public View findSnapView(RecyclerView.LayoutManager layoutManager) { 86b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int childCount = layoutManager.getChildCount(); 87b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (childCount == 0) { 88b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return null; 89b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 90b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 91b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen OrientationHelper orientationHelper = getOrientationHelper(layoutManager); 92b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 93b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // If there's only one child, then that will be the snap target. 94b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (childCount == 1) { 95b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen View firstChild = layoutManager.getChildAt(0); 96b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen return isValidSnapView(firstChild, orientationHelper) ? firstChild : null; 97b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 98b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 99b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen View lastVisibleChild = layoutManager.getChildAt(childCount - 1); 100b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 101b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // Check if the last child visible is the last item in the list. 102b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen boolean lastItemVisible = 103b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen layoutManager.getPosition(lastVisibleChild) == layoutManager.getItemCount() - 1; 104b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 105b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // If it is, then check how much of that view is visible. 106b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen float lastItemPercentageVisible = lastItemVisible 107b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen ? getPercentageVisible(lastVisibleChild, orientationHelper) : 0; 108b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 109b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen View closestChild = null; 110b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int closestDistanceToStart = Integer.MAX_VALUE; 111b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen float closestPercentageVisible = 0.f; 112b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 113b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // Iterate to find the child closest to the top and more than half way visible. 114b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen for (int i = 0; i < childCount; i++) { 115b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen View child = layoutManager.getChildAt(i); 116b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int startOffset = orientationHelper.getDecoratedStart(child); 117b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 118b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (Math.abs(startOffset) < closestDistanceToStart) { 119b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen float percentageVisible = getPercentageVisible(child, orientationHelper); 120b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 121b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (percentageVisible > VIEW_VISIBLE_THRESHOLD 122b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen && percentageVisible > closestPercentageVisible) { 123b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen closestDistanceToStart = startOffset; 124b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen closestChild = child; 125b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen closestPercentageVisible = percentageVisible; 126b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 127b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 128b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 129b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 130b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen View childToReturn = closestChild; 131b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 132b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // If closestChild is null, then that means we were unable to find a closest child that 133b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // is over the VIEW_VISIBLE_THRESHOLD. This could happen if the views are larger than 134b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // the given area. In this case, consider returning the lastVisibleChild so that the screen 135b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // scrolls. Also, check if the last item should be displayed anyway if it is mostly visible. 136b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen if ((childToReturn == null 137b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen || (lastItemVisible && lastItemPercentageVisible > closestPercentageVisible))) { 138b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen childToReturn = lastVisibleChild; 139b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen } 140b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 141b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // Return null if the childToReturn is not valid. This allows the user to scroll freely 142b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // with no snapping. This can allow them to see the entire view. 143b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen return isValidSnapView(childToReturn, orientationHelper) ? childToReturn : null; 144b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen } 145b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 146b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen /** 147b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * Returns whether or not the given View is a valid snapping view. A view is considered valid 148b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * for snapping if: 149b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * 150b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * <ol> 151b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * <li>It can fit entirely within the height of the RecyclerView it is contained within. 152b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * <li>Or the start of the view is not above the start of the RecyclerView. 153b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * </ol> 154b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * 155b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * <p>If the view is larger than the RecyclerView, then it might not want to be snapped to 156b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * to allow the user to scroll and see the rest of the View. Likewise, if the start of the 157b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * view is above the start of the RecyclerView, it implies that the user has already scrolled 158b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * past it and might want to see the rest of it. Thus, the view should not be snapped to. 159b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * 160b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * @param view The view to determine the snapping potential. 161b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * @param helper The {@link OrientationHelper} associated with the current RecyclerView. 162b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * @return {@code true} if the given view is a valid snapping view; {@code false} otherwise. 163b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen */ 164b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen private boolean isValidSnapView(View view, OrientationHelper helper) { 165b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen return helper.getDecoratedMeasurement(view) <= helper.getLayoutManager().getHeight() 166b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen || helper.getDecoratedStart(view) >= 0; 167b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 168b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 169b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** 170b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Returns the percentage of the given view that is visible, relative to its containing 171b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * RecyclerView. 172b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 173b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param view The View to get the percentage visible of. 174b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param helper An {@link OrientationHelper} to aid with calculation. 175b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @return A float indicating the percentage of the given view that is visible. 176b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 177b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen private float getPercentageVisible(View view, OrientationHelper helper) { 178b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int start = 0; 179b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int end = helper.getEnd(); 180b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 181b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int viewStart = helper.getDecoratedStart(view); 182b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int viewEnd = helper.getDecoratedEnd(view); 183b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 184b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (viewStart >= start && viewEnd <= end) { 185b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // The view is within the bounds of the RecyclerView, so it's fully visible. 186b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return 1.f; 187b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } else if (viewStart <= start && viewEnd >= end) { 188b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // The view is larger than the height of the RecyclerView. 189b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int viewHeight = helper.getDecoratedMeasurement(view); 190b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return 1.f - ((float) (Math.abs(viewStart) + Math.abs(viewEnd)) / viewHeight); 191b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } else if (viewStart < start) { 192b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // The view is above the start of the RecyclerView, so subtract the start offset 193b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // from the total height. 194b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return 1.f - ((float) Math.abs(viewStart) / helper.getDecoratedMeasurement(view)); 195b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } else { 196b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // The view is below the end of the RecyclerView, so subtract the end offset from the 197b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // total height. 198b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return 1.f - ((float) Math.abs(viewEnd) / helper.getDecoratedMeasurement(view)); 199b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 200b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 201b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 202b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @Override 203b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen public void attachToRecyclerView(@Nullable RecyclerView recyclerView) { 204b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen super.attachToRecyclerView(recyclerView); 205b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen mRecyclerView = recyclerView; 206b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 207b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 208b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** 209f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * Returns a scroller specific to this {@code PagedSnapHelper}. This scroller is used for all 210f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * smooth scrolling operations, including flings. 211f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * 212f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached 213f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * {@link RecyclerView}. 214f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * 215f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen * @return a {@link RecyclerView.SmoothScroller} which will handle the scrolling. 216f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen */ 217f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen @Override 218f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen protected RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager) { 219f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen return mSmoothScroller; 220f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen } 221f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen 222f990b0b9c0d7851e15b5eb2f5014c2f6a010cb2aAnthony Chen /** 223b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Calculate the estimated scroll distance in each direction given velocities on both axes. 224b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * This method will clamp the maximum scroll distance so that a single fling will never scroll 225b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * more than one page. 226b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 227b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param velocityX Fling velocity on the horizontal axis. 228b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param velocityY Fling velocity on the vertical axis. 229b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @return An array holding the calculated distances in x and y directions respectively. 230b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 231b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @Override 232b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen public int[] calculateScrollDistance(int velocityX, int velocityY) { 233b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int[] outDist = super.calculateScrollDistance(velocityX, velocityY); 234b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 235b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (mRecyclerView == null) { 236b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return outDist; 237b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 238b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 239b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); 240b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (layoutManager == null || layoutManager.getChildCount() == 0) { 241b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return outDist; 242b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 243b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 244b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int lastChildPosition = isAtEnd(layoutManager) ? 0 : layoutManager.getChildCount() - 1; 245b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 246b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen OrientationHelper orientationHelper = getOrientationHelper(layoutManager); 247b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen View lastChild = layoutManager.getChildAt(lastChildPosition); 248b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen float percentageVisible = getPercentageVisible(lastChild, orientationHelper); 249b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 250b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen int maxDistance = layoutManager.getHeight(); 251b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen if (percentageVisible > 0.f) { 252b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // The max and min distance is the total height of the RecyclerView minus the height of 253b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // the last child. This ensures that each scroll will never scroll more than a single 254b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // page on the RecyclerView. That is, the max scroll will make the last child the 255b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen // first child and vice versa when scrolling the opposite way. 256b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen maxDistance -= layoutManager.getDecoratedMeasuredHeight(lastChild); 257b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen } 258b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 259b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int minDistance = -maxDistance; 260b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 261b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen outDist[0] = clamp(outDist[0], minDistance, maxDistance); 262b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen outDist[1] = clamp(outDist[1], minDistance, maxDistance); 263b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 264b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return outDist; 265b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 266b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 267b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** Returns {@code true} if the RecyclerView is completely displaying the first item. */ 268b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen public boolean isAtStart(RecyclerView.LayoutManager layoutManager) { 269b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (layoutManager == null || layoutManager.getChildCount() == 0) { 270b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return true; 271b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 272b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 273b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen View firstChild = layoutManager.getChildAt(0); 274b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen OrientationHelper orientationHelper = layoutManager.canScrollVertically() 275b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen ? getVerticalHelper(layoutManager) 276b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen : getHorizontalHelper(layoutManager); 277b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 278b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // Check that the first child is completely visible and is the first item in the list. 279b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return orientationHelper.getDecoratedStart(firstChild) >= 0 280b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen && layoutManager.getPosition(firstChild) == 0; 281b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 282b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 283b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** Returns {@code true} if the RecyclerView is completely displaying the last item. */ 284b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen public boolean isAtEnd(RecyclerView.LayoutManager layoutManager) { 285b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (layoutManager == null || layoutManager.getChildCount() == 0) { 286b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return true; 287b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 288b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 289b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen int childCount = layoutManager.getChildCount(); 290b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen View lastVisibleChild = layoutManager.getChildAt(childCount - 1); 291b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 292b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // The list has reached the bottom if the last child that is visible is the last item 293b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen // in the list and it's fully shown. 294b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return layoutManager.getPosition(lastVisibleChild) == (layoutManager.getItemCount() - 1) 295b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen && layoutManager.getDecoratedBottom(lastVisibleChild) <= layoutManager.getHeight(); 296b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 297b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 298b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen /** 299b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * Returns an {@link OrientationHelper} that corresponds to the current scroll direction of 300b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen * the given {@link android.support.v7.widget.RecyclerView.LayoutManager}. 301b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen */ 302b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen @NonNull 303b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen private OrientationHelper getOrientationHelper( 304b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen @NonNull RecyclerView.LayoutManager layoutManager) { 305b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen return layoutManager.canScrollVertically() 306b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen ? getVerticalHelper(layoutManager) 307b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen : getHorizontalHelper(layoutManager); 308b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen } 309b1df1da479733666c3c877ff3fdc602f9d3f271aAnthony Chen 310b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @NonNull 311b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) { 312b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (mVerticalHelper == null || mVerticalHelper.getLayoutManager() != layoutManager) { 313b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager); 314b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 315b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return mVerticalHelper; 316b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 317b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 318b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @NonNull 319b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen private OrientationHelper getHorizontalHelper( 320b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen @NonNull RecyclerView.LayoutManager layoutManager) { 321b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen if (mHorizontalHelper == null || mHorizontalHelper.getLayoutManager() != layoutManager) { 322b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager); 323b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 324b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return mHorizontalHelper; 325b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 326b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen 327b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen /** 328b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * Ensures that the given value falls between the range given by the min and max values. This 329b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * method does not check that the min value is greater than or equal to the max value. If the 330b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * parameters are not well-formed, this method's behavior is undefined. 331b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * 332b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param value The value to clamp. 333b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param min The minimum value the given value can be. 334b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @param max The maximum value the given value can be. 335b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * @return A number that falls between {@code min} or {@code max} or one of those values if the 336b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen * given value is less than or greater than {@code min} and {@code max} respectively. 337b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen */ 338b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen private static int clamp(int value, int min, int max) { 339b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen return Math.max(min, Math.min(max, value)); 340b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen } 341b226d4dcc396fe8837876dfa113143702dd23c26Anthony Chen} 342