blob: c16b319934959307ade6d6f2434ccb5b24243c1c [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;
vadimt31fa6532020-04-14 19:21:07 -070023import android.view.accessibility.AccessibilityNodeInfo;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070024
Andy Wickham2ba77972022-05-17 00:01:54 +000025import androidx.annotation.Nullable;
Samuel Fufaf5a4deb2020-03-04 16:24:06 -080026import androidx.recyclerview.widget.RecyclerView;
27
vadimt758a1d92019-09-04 12:08:27 -070028import com.android.launcher3.compat.AccessibilityManagerCompat;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070029import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070030
Winson Chungb1777442015-06-16 13:35:04 -070031
Winson Chung94804152015-05-08 13:06:44 -070032/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070033 * A base {@link RecyclerView}, which does the following:
34 * <ul>
35 * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold.
36 * <li> Enable fast scroller.
37 * </ul>
Winson Chung94804152015-05-08 13:06:44 -070038 */
Andy Wickham2ba77972022-05-17 00:01:54 +000039public abstract class FastScrollRecyclerView extends RecyclerView {
Winson Chung94804152015-05-08 13:06:44 -070040
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070041 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070042
Andy Wickham2ba77972022-05-17 00:01:54 +000043 public FastScrollRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070044 this(context, null);
45 }
46
Andy Wickham2ba77972022-05-17 00:01:54 +000047 public FastScrollRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070048 this(context, attrs, 0);
49 }
50
Andy Wickham2ba77972022-05-17 00:01:54 +000051 public FastScrollRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070052 super(context, attrs, defStyleAttr);
Winson Chung94804152015-05-08 13:06:44 -070053 }
54
Sunny Goyal4d18ad42023-01-30 16:54:31 -080055 public void bindFastScrollbar(RecyclerViewFastScroller scrollbar) {
56 mScrollbar = scrollbar;
57 mScrollbar.setRecyclerView(this);
Mario Bertschler2d3157a2017-11-27 13:10:44 -080058 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070059 }
60
Andy Wickham2ba77972022-05-17 00:01:54 +000061 @Nullable
Sunny Goyal3661bfa2018-03-01 16:29:15 -080062 public RecyclerViewFastScroller getScrollbar() {
63 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070064 }
65
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070066 public int getScrollBarTop() {
67 return getPaddingTop();
68 }
69
Andy Wickham03fdeb72023-01-13 15:38:49 -080070 public int getScrollBarMarginBottom() {
71 return getPaddingBottom();
72 }
73
Winson Chung94804152015-05-08 13:06:44 -070074 /**
Sunny Goyal00e10682016-10-18 14:23:46 +010075 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -070076 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070077 public int getScrollbarTrackHeight() {
Andy Wickham03fdeb72023-01-13 15:38:49 -080078 return mScrollbar.getHeight() - getScrollBarTop() - getScrollBarMarginBottom();
Winsonc0880492015-08-21 11:16:27 -070079 }
80
81 /**
Winson Chungb1777442015-06-16 13:35:04 -070082 * Returns the available scroll height:
83 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -070084 */
Sunny Goyalbbad97e2022-05-26 07:12:43 -070085 protected int getAvailableScrollHeight() {
86 // AvailableScrollHeight = Total height of the all items - first page height
87 int firstPageHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
Sunny Goyalf34811d2022-10-21 20:42:34 +000088 int availableScrollHeight = computeVerticalScrollRange() - firstPageHeight;
Sunny Goyalbbad97e2022-05-26 07:12:43 -070089 return Math.max(0, availableScrollHeight);
90 }
Winson Chungb1777442015-06-16 13:35:04 -070091
92 /**
93 * Returns the available scroll bar height:
94 * AvailableScrollBarHeight = Total height of the visible view - thumb height
95 */
96 protected int getAvailableScrollBarHeight() {
Sunny Goyalbbad97e2022-05-26 07:12:43 -070097 return getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -070098 }
99
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700100 /**
Winson Chungb1777442015-06-16 13:35:04 -0700101 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
102 * this by mapping the available scroll area of the recycler view to the available space for the
103 * scroll bar.
104 *
Winsonb655b882016-07-11 18:59:18 -0700105 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700106 */
Winsonb655b882016-07-11 18:59:18 -0700107 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
108 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700109 // Only show the scrollbar if there is height to be scrolled
110 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700111 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700112 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700113 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700114
Winson Chungb1777442015-06-16 13:35:04 -0700115 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
116 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
117 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100118 int scrollBarY =
119 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700120
Winson Chungb1777442015-06-16 13:35:04 -0700121 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700122 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700123 }
124
125 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700126 * Returns whether the view itself will handle the touch event or not.
127 * @param ev MotionEvent in {@param eventSource}
128 */
129 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700130 float[] point = new float[2];
131 point[0] = ev.getX();
132 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700133 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
134 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700135 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700136 return false;
137 }
138
139 // IF scroller is at the very top OR there is no scroll bar because there is probably not
140 // enough items to scroll, THEN it's okay for the container to be pulled down.
Sunny Goyalf34811d2022-10-21 20:42:34 +0000141 return computeVerticalScrollOffset() == 0;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700142 }
143
144 /**
Winsonc0880492015-08-21 11:16:27 -0700145 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700146 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700147 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700148 return true;
149 }
150
151 /**
Winsonc0880492015-08-21 11:16:27 -0700152 * Maps the touch (from 0..1) to the adapter position that should be visible.
153 * <p>Override in each subclass of this base class.
154 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700155 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700156
157 /**
158 * Updates the bounds for the scrollbar.
159 * <p>Override in each subclass of this base class.
160 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700161 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700162
163 /**
Winson Chungb1777442015-06-16 13:35:04 -0700164 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700165 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700166 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700167
168 @Override
169 public void onScrollStateChanged(int state) {
170 super.onScrollStateChanged(state);
171
172 if (state == SCROLL_STATE_IDLE) {
173 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
174 }
175 }
vadimt31fa6532020-04-14 19:21:07 -0700176
177 @Override
178 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
179 super.onInitializeAccessibilityNodeInfo(info);
180 if (isLayoutSuppressed()) info.setScrollable(false);
vadimt31fa6532020-04-14 19:21:07 -0700181 }
Steven Ng167f81b2021-02-23 10:48:46 +0000182
183 /**
184 * Scrolls this recycler view to the top.
185 */
186 public void scrollToTop() {
187 if (mScrollbar != null) {
188 mScrollbar.reattachThumbToScroll();
189 }
Steven Ng167f81b2021-02-23 10:48:46 +0000190 scrollToPosition(0);
191 }
Andy Wickham2ba77972022-05-17 00:01:54 +0000192}