blob: a13dcc1bc3178c08c5c7a417c02c286ea0660c11 [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
Jagrut Desai311d5192023-09-13 10:18:02 -070019import static com.android.launcher3.testing.shared.TestProtocol.SCROLL_FINISHED_MESSAGE;
20
Winson Chung94804152015-05-08 13:06:44 -070021import android.content.Context;
Winson Chung94804152015-05-08 13:06:44 -070022import android.util.AttributeSet;
23import android.view.MotionEvent;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070024import android.view.View;
vadimt31fa6532020-04-14 19:21:07 -070025import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070026
Andy Wickham2ba77972022-05-17 00:01:54 +000027import androidx.annotation.Nullable;
Samuel Fufaf5a4deb2020-03-04 16:24:06 -080028import androidx.recyclerview.widget.RecyclerView;
29
Holly Sun454590d2023-11-27 13:39:38 -080030import com.android.app.animation.Interpolators;
vadimt758a1d92019-09-04 12:08:27 -070031import com.android.launcher3.compat.AccessibilityManagerCompat;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070032import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070033
Winson Chungb1777442015-06-16 13:35:04 -070034
Winson Chung94804152015-05-08 13:06:44 -070035/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070036 * A base {@link RecyclerView}, which does the following:
37 * <ul>
38 * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold.
39 * <li> Enable fast scroller.
40 * </ul>
Winson Chung94804152015-05-08 13:06:44 -070041 */
Andy Wickham2ba77972022-05-17 00:01:54 +000042public abstract class FastScrollRecyclerView extends RecyclerView {
Winson Chung94804152015-05-08 13:06:44 -070043
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070044 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070045
Andy Wickham2ba77972022-05-17 00:01:54 +000046 public FastScrollRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070047 this(context, null);
48 }
49
Andy Wickham2ba77972022-05-17 00:01:54 +000050 public FastScrollRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070051 this(context, attrs, 0);
52 }
53
Andy Wickham2ba77972022-05-17 00:01:54 +000054 public FastScrollRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070055 super(context, attrs, defStyleAttr);
Winson Chung94804152015-05-08 13:06:44 -070056 }
57
Sunny Goyal4d18ad42023-01-30 16:54:31 -080058 public void bindFastScrollbar(RecyclerViewFastScroller scrollbar) {
59 mScrollbar = scrollbar;
60 mScrollbar.setRecyclerView(this);
Mario Bertschler2d3157a2017-11-27 13:10:44 -080061 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070062 }
63
Andy Wickham2ba77972022-05-17 00:01:54 +000064 @Nullable
Sunny Goyal3661bfa2018-03-01 16:29:15 -080065 public RecyclerViewFastScroller getScrollbar() {
66 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070067 }
68
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070069 public int getScrollBarTop() {
70 return getPaddingTop();
71 }
72
Andy Wickham03fdeb72023-01-13 15:38:49 -080073 public int getScrollBarMarginBottom() {
74 return getPaddingBottom();
75 }
76
Winson Chung94804152015-05-08 13:06:44 -070077 /**
Sunny Goyal00e10682016-10-18 14:23:46 +010078 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -070079 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070080 public int getScrollbarTrackHeight() {
Andy Wickham03fdeb72023-01-13 15:38:49 -080081 return mScrollbar.getHeight() - getScrollBarTop() - getScrollBarMarginBottom();
Winsonc0880492015-08-21 11:16:27 -070082 }
83
84 /**
Winson Chungb1777442015-06-16 13:35:04 -070085 * Returns the available scroll height:
86 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -070087 */
Sunny Goyalbbad97e2022-05-26 07:12:43 -070088 protected int getAvailableScrollHeight() {
89 // AvailableScrollHeight = Total height of the all items - first page height
90 int firstPageHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Sunny Goyalf34811d2022-10-21 20:42:34 +000091 int availableScrollHeight = computeVerticalScrollRange() - firstPageHeight;
Sunny Goyalbbad97e2022-05-26 07:12:43 -070092 return Math.max(0, availableScrollHeight);
93 }
Winson Chungb1777442015-06-16 13:35:04 -070094
95 /**
96 * Returns the available scroll bar height:
97 * AvailableScrollBarHeight = Total height of the visible view - thumb height
98 */
99 protected int getAvailableScrollBarHeight() {
Sunny Goyalbbad97e2022-05-26 07:12:43 -0700100 return getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700101 }
102
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700103 /**
Winson Chungb1777442015-06-16 13:35:04 -0700104 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
105 * this by mapping the available scroll area of the recycler view to the available space for the
106 * scroll bar.
107 *
Winsonb655b882016-07-11 18:59:18 -0700108 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700109 */
Winsonb655b882016-07-11 18:59:18 -0700110 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
111 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700112 // Only show the scrollbar if there is height to be scrolled
113 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700114 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700115 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700116 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700117
Winson Chungb1777442015-06-16 13:35:04 -0700118 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
119 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
120 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100121 int scrollBarY =
122 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700123
Winson Chungb1777442015-06-16 13:35:04 -0700124 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700125 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700126 }
127
128 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700129 * Returns whether the view itself will handle the touch event or not.
130 * @param ev MotionEvent in {@param eventSource}
131 */
132 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700133 float[] point = new float[2];
134 point[0] = ev.getX();
135 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700136 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
137 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700138 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139 return false;
140 }
141
142 // IF scroller is at the very top OR there is no scroll bar because there is probably not
143 // enough items to scroll, THEN it's okay for the container to be pulled down.
Sunny Goyalf34811d2022-10-21 20:42:34 +0000144 return computeVerticalScrollOffset() == 0;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700145 }
146
147 /**
Winsonc0880492015-08-21 11:16:27 -0700148 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700149 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700150 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700151 return true;
152 }
153
154 /**
Winsonc0880492015-08-21 11:16:27 -0700155 * Maps the touch (from 0..1) to the adapter position that should be visible.
156 * <p>Override in each subclass of this base class.
157 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700158 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700159
160 /**
161 * Updates the bounds for the scrollbar.
162 * <p>Override in each subclass of this base class.
163 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700164 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700165
166 /**
Winson Chungb1777442015-06-16 13:35:04 -0700167 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700168 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700169 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700170
171 @Override
172 public void onScrollStateChanged(int state) {
173 super.onScrollStateChanged(state);
174
175 if (state == SCROLL_STATE_IDLE) {
Jagrut Desai311d5192023-09-13 10:18:02 -0700176 AccessibilityManagerCompat.sendTestProtocolEventToTest(getContext(),
177 SCROLL_FINISHED_MESSAGE);
vadimt758a1d92019-09-04 12:08:27 -0700178 }
179 }
vadimt31fa6532020-04-14 19:21:07 -0700180
181 @Override
182 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
183 super.onInitializeAccessibilityNodeInfo(info);
184 if (isLayoutSuppressed()) info.setScrollable(false);
vadimt31fa6532020-04-14 19:21:07 -0700185 }
Steven Ng167f81b2021-02-23 10:48:46 +0000186
187 /**
188 * Scrolls this recycler view to the top.
189 */
190 public void scrollToTop() {
191 if (mScrollbar != null) {
192 mScrollbar.reattachThumbToScroll();
193 }
Steven Ng167f81b2021-02-23 10:48:46 +0000194 scrollToPosition(0);
195 }
Holly Sund670f302023-11-07 14:29:23 -0800196
197 /**
Holly Sun454590d2023-11-27 13:39:38 -0800198 * Scrolls this recycler view to the bottom with easing and duration.
Holly Sund670f302023-11-07 14:29:23 -0800199 */
Holly Sun454590d2023-11-27 13:39:38 -0800200 public void scrollToBottomWithMotion() {
Holly Sund670f302023-11-07 14:29:23 -0800201 if (mScrollbar != null) {
202 mScrollbar.reattachThumbToScroll();
203 }
Holly Sun454590d2023-11-27 13:39:38 -0800204 // Emphasized interpolators with 500ms duration
205 smoothScrollBy(0, getAvailableScrollHeight(), Interpolators.EMPHASIZED, 500);
Holly Sund670f302023-11-07 14:29:23 -0800206 }
Andy Wickham2ba77972022-05-17 00:01:54 +0000207}