blob: 9e8441a0dd9d3914a8f4ce41afb6dc2de65cfa30 [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;
vadimtac752e32020-03-24 17:58:03 -070021import android.util.Log;
Winson Chung94804152015-05-08 13:06:44 -070022import android.view.MotionEvent;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070023import android.view.View;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070024import android.view.ViewGroup;
25
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;
vadimtac752e32020-03-24 17:58:03 -070029import com.android.launcher3.testing.TestProtocol;
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 */
Sunny Goyal3661bfa2018-03-01 16:29:15 -080040public abstract class BaseRecyclerView 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
Winson Chung5f4e0fd2015-05-22 11:12:27 -070044 public BaseRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070045 this(context, null);
46 }
47
Winson Chung5f4e0fd2015-05-22 11:12:27 -070048 public BaseRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070049 this(context, attrs, 0);
50 }
51
Winson Chung5f4e0fd2015-05-22 11:12:27 -070052 public BaseRecyclerView(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();
Mario Bertschlerac9408a2017-10-18 10:31:36 -070059 bindFastScrollbar();
60 }
61
62 public void bindFastScrollbar() {
63 ViewGroup parent = (ViewGroup) getParent().getParent();
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070064 mScrollbar = parent.findViewById(R.id.fast_scroller);
Mario Bertschlerac9408a2017-10-18 10:31:36 -070065 mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup));
Mario Bertschler2d3157a2017-11-27 13:10:44 -080066 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070067 }
68
Sunny Goyal3661bfa2018-03-01 16:29:15 -080069 public RecyclerViewFastScroller getScrollbar() {
70 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070071 }
72
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070073 public int getScrollBarTop() {
74 return getPaddingTop();
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() {
Mario Bertschler2d3157a2017-11-27 13:10:44 -080081 return mScrollbar.getHeight() - getScrollBarTop() - getPaddingBottom();
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 */
Winsonb655b882016-07-11 18:59:18 -070088 protected abstract int getAvailableScrollHeight();
Winson Chungb1777442015-06-16 13:35:04 -070089
90 /**
91 * Returns the available scroll bar height:
92 * AvailableScrollBarHeight = Total height of the visible view - thumb height
93 */
94 protected int getAvailableScrollBarHeight() {
Sunny Goyal00e10682016-10-18 14:23:46 +010095 int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -070096 return availableScrollBarHeight;
97 }
98
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070099 /**
Winson Chungb1777442015-06-16 13:35:04 -0700100 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
101 * this by mapping the available scroll area of the recycler view to the available space for the
102 * scroll bar.
103 *
Winsonb655b882016-07-11 18:59:18 -0700104 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700105 */
Winsonb655b882016-07-11 18:59:18 -0700106 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
107 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700108 // Only show the scrollbar if there is height to be scrolled
109 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700110 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700111 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700112 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700113
Winson Chungb1777442015-06-16 13:35:04 -0700114 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
115 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
116 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100117 int scrollBarY =
118 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700119
Winson Chungb1777442015-06-16 13:35:04 -0700120 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700121 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700122 }
123
124 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700125 * Returns whether the view itself will handle the touch event or not.
126 * @param ev MotionEvent in {@param eventSource}
127 */
128 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700129 float[] point = new float[2];
130 point[0] = ev.getX();
131 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700132 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
133 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700134 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700135 return false;
136 }
137
138 // IF scroller is at the very top OR there is no scroll bar because there is probably not
139 // enough items to scroll, THEN it's okay for the container to be pulled down.
140 if (getCurrentScrollY() == 0) {
141 return true;
142 }
Samuel Fufaf5a4deb2020-03-04 16:24:06 -0800143 return getAdapter() == null || getAdapter().getItemCount() == 0;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700144 }
145
146 /**
Winsonc0880492015-08-21 11:16:27 -0700147 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700148 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700149 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700150 return true;
151 }
152
153 /**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700154 * Maps the touch (from 0..1) to the adapter position that should be visible.
155 * <p>Override in each subclass of this base class.
Winsonc0880492015-08-21 11:16:27 -0700156 *
157 * @return the scroll top of this recycler view.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700158 */
Peter Schiller2e22b5d2016-07-12 12:45:10 -0700159 public abstract int getCurrentScrollY();
Winsonc0880492015-08-21 11:16:27 -0700160
161 /**
162 * Maps the touch (from 0..1) to the adapter position that should be visible.
163 * <p>Override in each subclass of this base class.
164 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700165 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700166
167 /**
168 * Updates the bounds for the scrollbar.
169 * <p>Override in each subclass of this base class.
170 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700171 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700172
173 /**
Winson Chungb1777442015-06-16 13:35:04 -0700174 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700175 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700176 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700177
178 @Override
179 public void onScrollStateChanged(int state) {
180 super.onScrollStateChanged(state);
181
vadimtac752e32020-03-24 17:58:03 -0700182 if (TestProtocol.sDebugTracing) {
183 Log.d(TestProtocol.NO_SCROLL_END_WIDGETS, "onScrollStateChanged: " + state);
184 }
185
vadimt758a1d92019-09-04 12:08:27 -0700186 if (state == SCROLL_STATE_IDLE) {
187 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
188 }
189 }
Winson Chung94804152015-05-08 13:06:44 -0700190}