Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 com.android.launcher3; |
| 18 | |
| 19 | import android.content.Context; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 20 | import android.util.AttributeSet; |
| 21 | import android.view.MotionEvent; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 22 | import android.view.View; |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 23 | import android.view.ViewGroup; |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 24 | import android.view.accessibility.AccessibilityNodeInfo; |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 25 | |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 26 | import androidx.annotation.Nullable; |
Sunny Goyal | bbad97e | 2022-05-26 07:12:43 -0700 | [diff] [blame] | 27 | import androidx.recyclerview.widget.LinearLayoutManager; |
Samuel Fufa | f5a4deb | 2020-03-04 16:24:06 -0800 | [diff] [blame] | 28 | import androidx.recyclerview.widget.RecyclerView; |
| 29 | |
vadimt | 758a1d9 | 2019-09-04 12:08:27 -0700 | [diff] [blame] | 30 | import com.android.launcher3.compat.AccessibilityManagerCompat; |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 31 | import com.android.launcher3.views.RecyclerViewFastScroller; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 32 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 33 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 34 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 35 | * A base {@link RecyclerView}, which does the following: |
| 36 | * <ul> |
| 37 | * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold. |
| 38 | * <li> Enable fast scroller. |
| 39 | * </ul> |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 40 | */ |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 41 | public abstract class FastScrollRecyclerView extends RecyclerView { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 42 | |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 43 | protected RecyclerViewFastScroller mScrollbar; |
Sunny Goyal | 9e4c359 | 2017-06-07 14:05:08 -0700 | [diff] [blame] | 44 | |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 45 | public FastScrollRecyclerView(Context context) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 46 | this(context, null); |
| 47 | } |
| 48 | |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 49 | public FastScrollRecyclerView(Context context, AttributeSet attrs) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 50 | this(context, attrs, 0); |
| 51 | } |
| 52 | |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 53 | public FastScrollRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 54 | super(context, attrs, defStyleAttr); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 57 | @Override |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 58 | protected void onAttachedToWindow() { |
| 59 | super.onAttachedToWindow(); |
Andy Wickham | 87a9bf7 | 2022-07-08 20:31:22 -0700 | [diff] [blame^] | 60 | if (mScrollbar == null || !mScrollbar.hasRecyclerView()) { |
| 61 | bindFastScrollbar(); |
| 62 | } |
Mario Bertschler | ac9408a | 2017-10-18 10:31:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public void bindFastScrollbar() { |
| 66 | ViewGroup parent = (ViewGroup) getParent().getParent(); |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 67 | mScrollbar = parent.findViewById(R.id.fast_scroller); |
Mario Bertschler | ac9408a | 2017-10-18 10:31:36 -0700 | [diff] [blame] | 68 | mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup)); |
Mario Bertschler | 2d3157a | 2017-11-27 13:10:44 -0800 | [diff] [blame] | 69 | onUpdateScrollbar(0); |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 72 | @Nullable |
Sunny Goyal | 3661bfa | 2018-03-01 16:29:15 -0800 | [diff] [blame] | 73 | public RecyclerViewFastScroller getScrollbar() { |
| 74 | return mScrollbar; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 77 | public int getScrollBarTop() { |
| 78 | return getPaddingTop(); |
| 79 | } |
| 80 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 81 | /** |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 82 | * Returns the height of the fast scroll bar |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 83 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 84 | public int getScrollbarTrackHeight() { |
Mario Bertschler | 2d3157a | 2017-11-27 13:10:44 -0800 | [diff] [blame] | 85 | return mScrollbar.getHeight() - getScrollBarTop() - getPaddingBottom(); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 89 | * Returns the available scroll height: |
| 90 | * AvailableScrollHeight = Total height of the all items - last page height |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 91 | */ |
Sunny Goyal | bbad97e | 2022-05-26 07:12:43 -0700 | [diff] [blame] | 92 | protected int getAvailableScrollHeight() { |
| 93 | // AvailableScrollHeight = Total height of the all items - first page height |
| 94 | int firstPageHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); |
| 95 | int totalHeightOfAllItems = getItemsHeight(/* untilIndex= */ getAdapter().getItemCount()); |
| 96 | int availableScrollHeight = totalHeightOfAllItems - firstPageHeight; |
| 97 | return Math.max(0, availableScrollHeight); |
| 98 | } |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Returns the available scroll bar height: |
| 102 | * AvailableScrollBarHeight = Total height of the visible view - thumb height |
| 103 | */ |
| 104 | protected int getAvailableScrollBarHeight() { |
Sunny Goyal | bbad97e | 2022-05-26 07:12:43 -0700 | [diff] [blame] | 105 | return getScrollbarTrackHeight() - mScrollbar.getThumbHeight(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 108 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 109 | * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does |
| 110 | * this by mapping the available scroll area of the recycler view to the available space for the |
| 111 | * scroll bar. |
| 112 | * |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 113 | * @param scrollY the current scroll y |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 114 | */ |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 115 | protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY, |
| 116 | int availableScrollHeight) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 117 | // Only show the scrollbar if there is height to be scrolled |
| 118 | if (availableScrollHeight <= 0) { |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 119 | mScrollbar.setThumbOffsetY(-1); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 120 | return; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 121 | } |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 122 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 123 | // Calculate the current scroll position, the scrollY of the recycler view accounts for the |
| 124 | // view padding, while the scrollBarY is drawn right up to the background padding (ignoring |
| 125 | // padding) |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 126 | int scrollBarY = |
| 127 | (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight()); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 128 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 129 | // Calculate the position and size of the scroll bar |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 130 | mScrollbar.setThumbOffsetY(scrollBarY); |
Winson Chung | 4b9f979 | 2015-06-12 18:02:52 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 134 | * Returns whether the view itself will handle the touch event or not. |
| 135 | * @param ev MotionEvent in {@param eventSource} |
| 136 | */ |
| 137 | public boolean shouldContainerScroll(MotionEvent ev, View eventSource) { |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 138 | float[] point = new float[2]; |
| 139 | point[0] = ev.getX(); |
| 140 | point[1] = ev.getY(); |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 141 | Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point); |
| 142 | // IF the MotionEvent is inside the thumb, container should not be pulled down. |
Sunny Goyal | ae6e318 | 2019-04-30 12:04:37 -0700 | [diff] [blame] | 143 | if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // IF scroller is at the very top OR there is no scroll bar because there is probably not |
| 148 | // enough items to scroll, THEN it's okay for the container to be pulled down. |
| 149 | if (getCurrentScrollY() == 0) { |
| 150 | return true; |
| 151 | } |
Samuel Fufa | f5a4deb | 2020-03-04 16:24:06 -0800 | [diff] [blame] | 152 | return getAdapter() == null || getAdapter().getItemCount() == 0; |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 156 | * @return whether fast scrolling is supported in the current state. |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame] | 157 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 158 | public boolean supportsFastScrolling() { |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 163 | * @return the scroll top of this recycler view. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 164 | */ |
Sunny Goyal | bbad97e | 2022-05-26 07:12:43 -0700 | [diff] [blame] | 165 | public int getCurrentScrollY() { |
| 166 | Adapter adapter = getAdapter(); |
| 167 | if (adapter == null) { |
| 168 | return -1; |
| 169 | } |
| 170 | if (adapter.getItemCount() == 0 || getChildCount() == 0) { |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | int itemPosition = NO_POSITION; |
| 175 | View child = null; |
| 176 | |
| 177 | LayoutManager layoutManager = getLayoutManager(); |
| 178 | if (layoutManager instanceof LinearLayoutManager) { |
| 179 | // Use the LayoutManager as the source of truth for visible positions. During |
| 180 | // animations, the view group child may not correspond to the visible views that appear |
| 181 | // at the top. |
| 182 | itemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); |
| 183 | child = layoutManager.findViewByPosition(itemPosition); |
| 184 | } |
| 185 | |
| 186 | if (child == null) { |
| 187 | // If the layout manager returns null for any reason, which can happen before layout |
| 188 | // has occurred for the position, then look at the child of this view as a ViewGroup. |
| 189 | child = getChildAt(0); |
| 190 | itemPosition = getChildAdapterPosition(child); |
| 191 | } |
| 192 | if (itemPosition == NO_POSITION) { |
| 193 | return -1; |
| 194 | } |
| 195 | return getPaddingTop() + getItemsHeight(itemPosition) |
| 196 | - layoutManager.getDecoratedTop(child); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the sum of the height, in pixels, of this list adapter's items from index |
| 201 | * 0 (inclusive) until {@code untilIndex} (exclusive). If untilIndex is same as the itemCount, |
| 202 | * it returns the full height of all the items. |
| 203 | * |
| 204 | * <p>If the untilIndex is larger than the total number of items in this adapter, returns the |
| 205 | * sum of all items' height. |
| 206 | */ |
| 207 | protected abstract int getItemsHeight(int untilIndex); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Maps the touch (from 0..1) to the adapter position that should be visible. |
| 211 | * <p>Override in each subclass of this base class. |
| 212 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 213 | public abstract String scrollToPositionAtProgress(float touchFraction); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Updates the bounds for the scrollbar. |
| 217 | * <p>Override in each subclass of this base class. |
| 218 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 219 | public abstract void onUpdateScrollbar(int dy); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 222 | * <p>Override in each subclass of this base class. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 223 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 224 | public void onFastScrollCompleted() {} |
vadimt | 758a1d9 | 2019-09-04 12:08:27 -0700 | [diff] [blame] | 225 | |
| 226 | @Override |
| 227 | public void onScrollStateChanged(int state) { |
| 228 | super.onScrollStateChanged(state); |
| 229 | |
| 230 | if (state == SCROLL_STATE_IDLE) { |
| 231 | AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext()); |
| 232 | } |
| 233 | } |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 234 | |
| 235 | @Override |
| 236 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 237 | super.onInitializeAccessibilityNodeInfo(info); |
| 238 | if (isLayoutSuppressed()) info.setScrollable(false); |
vadimt | 31fa653 | 2020-04-14 19:21:07 -0700 | [diff] [blame] | 239 | } |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 240 | |
| 241 | /** |
| 242 | * Scrolls this recycler view to the top. |
| 243 | */ |
| 244 | public void scrollToTop() { |
| 245 | if (mScrollbar != null) { |
| 246 | mScrollbar.reattachThumbToScroll(); |
| 247 | } |
Steven Ng | 167f81b | 2021-02-23 10:48:46 +0000 | [diff] [blame] | 248 | scrollToPosition(0); |
| 249 | } |
Andy Wickham | 2ba7797 | 2022-05-17 00:01:54 +0000 | [diff] [blame] | 250 | } |