blob: e1a216e3d82548b9561f86fcf47adfb2ca55859c [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;
Samuel Fufaf5a4deb2020-03-04 16:24:06 -080027import androidx.recyclerview.widget.RecyclerView;
28
vadimt758a1d92019-09-04 12:08:27 -070029import com.android.launcher3.compat.AccessibilityManagerCompat;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070030import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070031
Winson Chungb1777442015-06-16 13:35:04 -070032
Winson Chung94804152015-05-08 13:06:44 -070033/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070034 * A base {@link RecyclerView}, which does the following:
35 * <ul>
36 * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold.
37 * <li> Enable fast scroller.
38 * </ul>
Winson Chung94804152015-05-08 13:06:44 -070039 */
Andy Wickham2ba77972022-05-17 00:01:54 +000040public abstract class FastScrollRecyclerView extends RecyclerView {
Winson Chung94804152015-05-08 13:06:44 -070041
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070042 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070043
Andy Wickham2ba77972022-05-17 00:01:54 +000044 public FastScrollRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070045 this(context, null);
46 }
47
Andy Wickham2ba77972022-05-17 00:01:54 +000048 public FastScrollRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070049 this(context, attrs, 0);
50 }
51
Andy Wickham2ba77972022-05-17 00:01:54 +000052 public FastScrollRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070053 super(context, attrs, defStyleAttr);
Winson Chung94804152015-05-08 13:06:44 -070054 }
55
Winson Chung94804152015-05-08 13:06:44 -070056 @Override
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070057 protected void onAttachedToWindow() {
58 super.onAttachedToWindow();
Andy Wickham87a9bf72022-07-08 20:31:22 -070059 if (mScrollbar == null || !mScrollbar.hasRecyclerView()) {
60 bindFastScrollbar();
61 }
Mario Bertschlerac9408a2017-10-18 10:31:36 -070062 }
63
64 public void bindFastScrollbar() {
65 ViewGroup parent = (ViewGroup) getParent().getParent();
Federico Baron4b1a38b2022-11-17 09:29:51 -080066 if (parent.findViewById(R.id.fast_scroller) == null) {
67 parent = (ViewGroup) parent.getParent();
68 }
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070069 mScrollbar = parent.findViewById(R.id.fast_scroller);
Mario Bertschlerac9408a2017-10-18 10:31:36 -070070 mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup));
Mario Bertschler2d3157a2017-11-27 13:10:44 -080071 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070072 }
73
Andy Wickham2ba77972022-05-17 00:01:54 +000074 @Nullable
Sunny Goyal3661bfa2018-03-01 16:29:15 -080075 public RecyclerViewFastScroller getScrollbar() {
76 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070077 }
78
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070079 public int getScrollBarTop() {
80 return getPaddingTop();
81 }
82
Andy Wickham03fdeb72023-01-13 15:38:49 -080083 public int getScrollBarMarginBottom() {
84 return getPaddingBottom();
85 }
86
Winson Chung94804152015-05-08 13:06:44 -070087 /**
Sunny Goyal00e10682016-10-18 14:23:46 +010088 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -070089 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070090 public int getScrollbarTrackHeight() {
Andy Wickham03fdeb72023-01-13 15:38:49 -080091 return mScrollbar.getHeight() - getScrollBarTop() - getScrollBarMarginBottom();
Winsonc0880492015-08-21 11:16:27 -070092 }
93
94 /**
Winson Chungb1777442015-06-16 13:35:04 -070095 * Returns the available scroll height:
96 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -070097 */
Sunny Goyalbbad97e2022-05-26 07:12:43 -070098 protected int getAvailableScrollHeight() {
99 // AvailableScrollHeight = Total height of the all items - first page height
100 int firstPageHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Sunny Goyalf34811d2022-10-21 20:42:34 +0000101 int availableScrollHeight = computeVerticalScrollRange() - firstPageHeight;
Sunny Goyalbbad97e2022-05-26 07:12:43 -0700102 return Math.max(0, availableScrollHeight);
103 }
Winson Chungb1777442015-06-16 13:35:04 -0700104
105 /**
106 * Returns the available scroll bar height:
107 * AvailableScrollBarHeight = Total height of the visible view - thumb height
108 */
109 protected int getAvailableScrollBarHeight() {
Sunny Goyalbbad97e2022-05-26 07:12:43 -0700110 return getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700111 }
112
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700113 /**
Winson Chungb1777442015-06-16 13:35:04 -0700114 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
115 * this by mapping the available scroll area of the recycler view to the available space for the
116 * scroll bar.
117 *
Winsonb655b882016-07-11 18:59:18 -0700118 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700119 */
Winsonb655b882016-07-11 18:59:18 -0700120 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
121 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700122 // Only show the scrollbar if there is height to be scrolled
123 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700124 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700125 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700126 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700127
Winson Chungb1777442015-06-16 13:35:04 -0700128 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
129 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
130 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100131 int scrollBarY =
132 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700133
Winson Chungb1777442015-06-16 13:35:04 -0700134 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700135 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700136 }
137
138 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700139 * Returns whether the view itself will handle the touch event or not.
140 * @param ev MotionEvent in {@param eventSource}
141 */
142 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700143 float[] point = new float[2];
144 point[0] = ev.getX();
145 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700146 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
147 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700148 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700149 return false;
150 }
151
152 // IF scroller is at the very top OR there is no scroll bar because there is probably not
153 // enough items to scroll, THEN it's okay for the container to be pulled down.
Sunny Goyalf34811d2022-10-21 20:42:34 +0000154 return computeVerticalScrollOffset() == 0;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700155 }
156
157 /**
Winsonc0880492015-08-21 11:16:27 -0700158 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700159 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700160 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700161 return true;
162 }
163
164 /**
Winsonc0880492015-08-21 11:16:27 -0700165 * Maps the touch (from 0..1) to the adapter position that should be visible.
166 * <p>Override in each subclass of this base class.
167 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700168 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700169
170 /**
171 * Updates the bounds for the scrollbar.
172 * <p>Override in each subclass of this base class.
173 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700174 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700175
176 /**
Winson Chungb1777442015-06-16 13:35:04 -0700177 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700178 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700179 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700180
181 @Override
182 public void onScrollStateChanged(int state) {
183 super.onScrollStateChanged(state);
184
185 if (state == SCROLL_STATE_IDLE) {
186 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
187 }
188 }
vadimt31fa6532020-04-14 19:21:07 -0700189
190 @Override
191 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
192 super.onInitializeAccessibilityNodeInfo(info);
193 if (isLayoutSuppressed()) info.setScrollable(false);
vadimt31fa6532020-04-14 19:21:07 -0700194 }
Steven Ng167f81b2021-02-23 10:48:46 +0000195
196 /**
197 * Scrolls this recycler view to the top.
198 */
199 public void scrollToTop() {
200 if (mScrollbar != null) {
201 mScrollbar.reattachThumbToScroll();
202 }
Steven Ng167f81b2021-02-23 10:48:46 +0000203 scrollToPosition(0);
204 }
Andy Wickham2ba77972022-05-17 00:01:54 +0000205}