blob: 38e1201b702b2a293d18fab526481ed5fa82377e [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;
24
Samuel Fufaf5a4deb2020-03-04 16:24:06 -080025import androidx.recyclerview.widget.RecyclerView;
26
vadimt758a1d92019-09-04 12:08:27 -070027import com.android.launcher3.compat.AccessibilityManagerCompat;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070028import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070029
Winson Chungb1777442015-06-16 13:35:04 -070030
Winson Chung94804152015-05-08 13:06:44 -070031/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070032 * A base {@link RecyclerView}, which does the following:
33 * <ul>
34 * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold.
35 * <li> Enable fast scroller.
36 * </ul>
Winson Chung94804152015-05-08 13:06:44 -070037 */
Sunny Goyal3661bfa2018-03-01 16:29:15 -080038public abstract class BaseRecyclerView extends RecyclerView {
Winson Chung94804152015-05-08 13:06:44 -070039
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070040 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070041
Winson Chung5f4e0fd2015-05-22 11:12:27 -070042 public BaseRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070043 this(context, null);
44 }
45
Winson Chung5f4e0fd2015-05-22 11:12:27 -070046 public BaseRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070047 this(context, attrs, 0);
48 }
49
Winson Chung5f4e0fd2015-05-22 11:12:27 -070050 public BaseRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
Winson Chung94804152015-05-08 13:06:44 -070051 super(context, attrs, defStyleAttr);
Winson Chung94804152015-05-08 13:06:44 -070052 }
53
Winson Chung94804152015-05-08 13:06:44 -070054 @Override
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070055 protected void onAttachedToWindow() {
56 super.onAttachedToWindow();
Mario Bertschlerac9408a2017-10-18 10:31:36 -070057 bindFastScrollbar();
58 }
59
60 public void bindFastScrollbar() {
61 ViewGroup parent = (ViewGroup) getParent().getParent();
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070062 mScrollbar = parent.findViewById(R.id.fast_scroller);
Mario Bertschlerac9408a2017-10-18 10:31:36 -070063 mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup));
Mario Bertschler2d3157a2017-11-27 13:10:44 -080064 onUpdateScrollbar(0);
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070065 }
66
Sunny Goyal3661bfa2018-03-01 16:29:15 -080067 public RecyclerViewFastScroller getScrollbar() {
68 return mScrollbar;
Winson Chung94804152015-05-08 13:06:44 -070069 }
70
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070071 public int getScrollBarTop() {
72 return getPaddingTop();
73 }
74
Winson Chung94804152015-05-08 13:06:44 -070075 /**
Sunny Goyal00e10682016-10-18 14:23:46 +010076 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -070077 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070078 public int getScrollbarTrackHeight() {
Mario Bertschler2d3157a2017-11-27 13:10:44 -080079 return mScrollbar.getHeight() - getScrollBarTop() - getPaddingBottom();
Winsonc0880492015-08-21 11:16:27 -070080 }
81
82 /**
Winson Chungb1777442015-06-16 13:35:04 -070083 * Returns the available scroll height:
84 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -070085 */
Winsonb655b882016-07-11 18:59:18 -070086 protected abstract int getAvailableScrollHeight();
Winson Chungb1777442015-06-16 13:35:04 -070087
88 /**
89 * Returns the available scroll bar height:
90 * AvailableScrollBarHeight = Total height of the visible view - thumb height
91 */
92 protected int getAvailableScrollBarHeight() {
Sunny Goyal00e10682016-10-18 14:23:46 +010093 int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -070094 return availableScrollBarHeight;
95 }
96
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070097 /**
Winson Chungb1777442015-06-16 13:35:04 -070098 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
99 * this by mapping the available scroll area of the recycler view to the available space for the
100 * scroll bar.
101 *
Winsonb655b882016-07-11 18:59:18 -0700102 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700103 */
Winsonb655b882016-07-11 18:59:18 -0700104 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
105 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700106 // Only show the scrollbar if there is height to be scrolled
107 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700108 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700109 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700110 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700111
Winson Chungb1777442015-06-16 13:35:04 -0700112 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
113 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
114 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100115 int scrollBarY =
116 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700117
Winson Chungb1777442015-06-16 13:35:04 -0700118 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700119 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700120 }
121
122 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700123 * Returns whether the view itself will handle the touch event or not.
124 * @param ev MotionEvent in {@param eventSource}
125 */
126 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
Sunny Goyalae6e3182019-04-30 12:04:37 -0700127 float[] point = new float[2];
128 point[0] = ev.getX();
129 point[1] = ev.getY();
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700130 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
131 // IF the MotionEvent is inside the thumb, container should not be pulled down.
Sunny Goyalae6e3182019-04-30 12:04:37 -0700132 if (mScrollbar.shouldBlockIntercept((int) point[0], (int) point[1])) {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700133 return false;
134 }
135
136 // IF scroller is at the very top OR there is no scroll bar because there is probably not
137 // enough items to scroll, THEN it's okay for the container to be pulled down.
138 if (getCurrentScrollY() == 0) {
139 return true;
140 }
Samuel Fufaf5a4deb2020-03-04 16:24:06 -0800141 return getAdapter() == null || getAdapter().getItemCount() == 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 /**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -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.
Winsonc0880492015-08-21 11:16:27 -0700154 *
155 * @return the scroll top of this recycler view.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700156 */
Peter Schiller2e22b5d2016-07-12 12:45:10 -0700157 public abstract int getCurrentScrollY();
Winsonc0880492015-08-21 11:16:27 -0700158
159 /**
160 * Maps the touch (from 0..1) to the adapter position that should be visible.
161 * <p>Override in each subclass of this base class.
162 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700163 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700164
165 /**
166 * Updates the bounds for the scrollbar.
167 * <p>Override in each subclass of this base class.
168 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700169 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700170
171 /**
Winson Chungb1777442015-06-16 13:35:04 -0700172 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700173 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700174 public void onFastScrollCompleted() {}
vadimt758a1d92019-09-04 12:08:27 -0700175
176 @Override
177 public void onScrollStateChanged(int state) {
178 super.onScrollStateChanged(state);
179
180 if (state == SCROLL_STATE_IDLE) {
181 AccessibilityManagerCompat.sendScrollFinishedEventToTest(getContext());
182 }
183 }
Winson Chung94804152015-05-08 13:06:44 -0700184}