blob: 94903f278ea460ec0532206ec5e2c6a129965cc2 [file] [log] [blame]
Winson Chung94804152015-05-08 13:06:44 -07001/*
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
17package com.android.launcher3;
18
19import android.content.Context;
Winson Chung94804152015-05-08 13:06:44 -070020import android.util.AttributeSet;
21import android.view.MotionEvent;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070022import android.view.View;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070023import android.view.ViewGroup;
vadimt31fa6532020-04-14 19:21:07 -070024import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070025
Andy Wickham2ba77972022-05-17 00:01:54 +000026import androidx.annotation.Nullable;
Sunny Goyalbbad97e2022-05-26 07:12:43 -070027import androidx.recyclerview.widget.LinearLayoutManager;
Samuel Fufaf5a4deb2020-03-04 16:24:06 -080028import androidx.recyclerview.widget.RecyclerView;
29
vadimt758a1d92019-09-04 12:08:27 -070030import com.android.launcher3.compat.AccessibilityManagerCompat;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070031import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070032
Winson Chungb1777442015-06-16 13:35:04 -070033
Winson Chung94804152015-05-08 13:06:44 -070034/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070035 * 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 Chung94804152015-05-08 13:06:44 -070040 */
Andy Wickham2ba77972022-05-17 00:01:54 +000041public abstract class FastScrollRecyclerView extends RecyclerView {
Winson Chung94804152015-05-08 13:06:44 -070042
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070043 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070044
Andy Wickham2ba77972022-05-17 00:01:54 +000045 public FastScrollRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070046 this(context, null);
47 }
48
Andy Wickham2ba77972022-05-17 00:01:54 +000049 public FastScrollRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070050 this(context, attrs, 0);
51 }
52
Andy Wickham2ba77972022-05-17 00:01:54 +000053 public FastScrollRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070054 super(context, attrs, defStyleAttr);
Winson Chung94804152015-05-08 13:06:44 -070055 }
56
Winson Chung94804152015-05-08 13:06:44 -070057 @Override
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070058 protected void onAttachedToWindow() {
59 super.onAttachedToWindow();
Mario Bertschlerac9408a2017-10-18 10:31:36 -070060 bindFastScrollbar();
61 }
62
63 public void bindFastScrollbar() {
64 ViewGroup parent = (ViewGroup) getParent().getParent();
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070065 mScrollbar = parent.findViewById(R.id.fast_scroller);
Mario Bertschlerac9408a2017-10-18 10:31:36 -070066 mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup));
Mario Bertschler2d3157a2017-11-27 13:10:44 -080067 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070068 }
69
Andy Wickham2ba77972022-05-17 00:01:54 +000070 @Nullable
Sunny Goyal3661bfa2018-03-01 16:29:15 -080071 public RecyclerViewFastScroller getScrollbar() {
72 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070073 }
74
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070075 public int getScrollBarTop() {
76 return getPaddingTop();
77 }
78
Winson Chung94804152015-05-08 13:06:44 -070079 /**
Sunny Goyal00e10682016-10-18 14:23:46 +010080 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -070081 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070082 public int getScrollbarTrackHeight() {
Mario Bertschler2d3157a2017-11-27 13:10:44 -080083 return mScrollbar.getHeight() - getScrollBarTop() - getPaddingBottom();
Winsonc0880492015-08-21 11:16:27 -070084 }
85
86 /**
Winson Chungb1777442015-06-16 13:35:04 -070087 * Returns the available scroll height:
88 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -070089 */
Sunny Goyalbbad97e2022-05-26 07:12:43 -070090 protected int getAvailableScrollHeight() {
91 // AvailableScrollHeight = Total height of the all items - first page height
92 int firstPageHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
93 int totalHeightOfAllItems = getItemsHeight(/* untilIndex= */ getAdapter().getItemCount());
94 int availableScrollHeight = totalHeightOfAllItems - firstPageHeight;
95 return Math.max(0, availableScrollHeight);
96 }
Winson Chungb1777442015-06-16 13:35:04 -070097
98 /**
99 * Returns the available scroll bar height:
100 * AvailableScrollBarHeight = Total height of the visible view - thumb height
101 */
102 protected int getAvailableScrollBarHeight() {
Sunny Goyalbbad97e2022-05-26 07:12:43 -0700103 return getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700104 }
105
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700106 /**
Winson Chungb1777442015-06-16 13:35:04 -0700107 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
108 * this by mapping the available scroll area of the recycler view to the available space for the
109 * scroll bar.
110 *
Winsonb655b882016-07-11 18:59:18 -0700111 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700112 */
Winsonb655b882016-07-11 18:59:18 -0700113 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
114 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700115 // Only show the scrollbar if there is height to be scrolled
116 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700117 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700118 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700119 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700120
Winson Chungb1777442015-06-16 13:35:04 -0700121 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
122 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
123 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100124 int scrollBarY =
125 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700126
Winson Chungb1777442015-06-16 13:35:04 -0700127 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700128 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700129 }
130
131 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700132 * Returns whether the view itself will handle the touch event or not.
133 * @param ev MotionEvent in {@param eventSource}
134 */
135 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700136 float[] point = new float[2];
137 point[0] = ev.getX();
138 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
140 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700141 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142 return false;
143 }
144
145 // IF scroller is at the very top OR there is no scroll bar because there is probably not
146 // enough items to scroll, THEN it's okay for the container to be pulled down.
147 if (getCurrentScrollY() == 0) {
148 return true;
149 }
Samuel Fufaf5a4deb2020-03-04 16:24:06 -0800150 return getAdapter() == null || getAdapter().getItemCount() == 0;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700151 }
152
153 /**
Winsonc0880492015-08-21 11:16:27 -0700154 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700155 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700156 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700157 return true;
158 }
159
160 /**
Winsonc0880492015-08-21 11:16:27 -0700161 * @return the scroll top of this recycler view.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700162 */
Sunny Goyalbbad97e2022-05-26 07:12:43 -0700163 public int getCurrentScrollY() {
164 Adapter adapter = getAdapter();
165 if (adapter == null) {
166 return -1;
167 }
168 if (adapter.getItemCount() == 0 || getChildCount() == 0) {
169 return -1;
170 }
171
172 int itemPosition = NO_POSITION;
173 View child = null;
174
175 LayoutManager layoutManager = getLayoutManager();
176 if (layoutManager instanceof LinearLayoutManager) {
177 // Use the LayoutManager as the source of truth for visible positions. During
178 // animations, the view group child may not correspond to the visible views that appear
179 // at the top.
180 itemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
181 child = layoutManager.findViewByPosition(itemPosition);
182 }
183
184 if (child == null) {
185 // If the layout manager returns null for any reason, which can happen before layout
186 // has occurred for the position, then look at the child of this view as a ViewGroup.
187 child = getChildAt(0);
188 itemPosition = getChildAdapterPosition(child);
189 }
190 if (itemPosition == NO_POSITION) {
191 return -1;
192 }
193 return getPaddingTop() + getItemsHeight(itemPosition)
194 - layoutManager.getDecoratedTop(child);
195 }
196
197 /**
198 * Returns the sum of the height, in pixels, of this list adapter's items from index
199 * 0 (inclusive) until {@code untilIndex} (exclusive). If untilIndex is same as the itemCount,
200 * it returns the full height of all the items.
201 *
202 * <p>If the untilIndex is larger than the total number of items in this adapter, returns the
203 * sum of all items' height.
204 */
205 protected abstract int getItemsHeight(int untilIndex);
Winsonc0880492015-08-21 11:16:27 -0700206
207 /**
208 * Maps the touch (from 0..1) to the adapter position that should be visible.
209 * <p>Override in each subclass of this base class.
210 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700211 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700212
213 /**
214 * Updates the bounds for the scrollbar.
215 * <p>Override in each subclass of this base class.
216 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700217 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700218
219 /**
Winson Chungb1777442015-06-16 13:35:04 -0700220 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700221 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700222 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700223
224 @Override
225 public void onScrollStateChanged(int state) {
226 super.onScrollStateChanged(state);
227
228 if (state == SCROLL_STATE_IDLE) {
229 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
230 }
231 }
vadimt31fa6532020-04-14 19:21:07 -0700232
233 @Override
234 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
235 super.onInitializeAccessibilityNodeInfo(info);
236 if (isLayoutSuppressed()) info.setScrollable(false);
vadimt31fa6532020-04-14 19:21:07 -0700237 }
Steven Ng167f81b2021-02-23 10:48:46 +0000238
239 /**
240 * Scrolls this recycler view to the top.
241 */
242 public void scrollToTop() {
243 if (mScrollbar != null) {
244 mScrollbar.reattachThumbToScroll();
245 }
Steven Ng167f81b2021-02-23 10:48:46 +0000246 scrollToPosition(0);
247 }
Andy Wickham2ba77972022-05-17 00:01:54 +0000248}