blob: afb83be580d36404e30ad7d4fd05d2d742fbbd73 [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;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070020import android.graphics.Canvas;
Winson Chung94804152015-05-08 13:06:44 -070021import android.support.v7.widget.RecyclerView;
22import android.util.AttributeSet;
23import android.view.MotionEvent;
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -070024import android.view.View;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070025import android.view.ViewGroup;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070026import android.widget.TextView;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070027
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 */
Winson Chungb1777442015-06-16 13:35:04 -070038public abstract class BaseRecyclerView extends RecyclerView
Winson Chung94804152015-05-08 13:06:44 -070039 implements RecyclerView.OnItemTouchListener {
40
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070041 protected RecyclerViewFastScroller mScrollbar;
Sunny Goyal9e4c3592017-06-07 14:05:08 -070042
Winson Chung5f4e0fd2015-05-22 11:12:27 -070043 public BaseRecyclerView(Context context) {
Winson Chung94804152015-05-08 13:06:44 -070044 this(context, null);
45 }
46
Winson Chung5f4e0fd2015-05-22 11:12:27 -070047 public BaseRecyclerView(Context context, AttributeSet attrs) {
Winson Chung94804152015-05-08 13:06:44 -070048 this(context, attrs, 0);
49 }
50
Winson Chung5f4e0fd2015-05-22 11:12:27 -070051 public BaseRecyclerView(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
Winson Chung94804152015-05-08 13:06:44 -070055 @Override
56 protected void onFinishInflate() {
57 super.onFinishInflate();
58 addOnItemTouchListener(this);
59 }
60
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070061 @Override
62 protected void onAttachedToWindow() {
63 super.onAttachedToWindow();
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070064 ViewGroup parent = (ViewGroup) getParent();
65 mScrollbar = parent.findViewById(R.id.fast_scroller);
66 mScrollbar.setRecyclerView(this, (TextView) parent.findViewById(R.id.fast_scroller_popup));
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070067 }
68
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070069 /**
70 * We intercept the touch handling only to support fast scrolling when initiated from the
71 * scroll bar. Otherwise, we fall back to the default RecyclerView touch handling.
72 */
Winson Chung94804152015-05-08 13:06:44 -070073 @Override
74 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070075 return handleTouchEvent(ev);
Winson Chung94804152015-05-08 13:06:44 -070076 }
77
78 @Override
79 public void onTouchEvent(RecyclerView rv, MotionEvent ev) {
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070080 handleTouchEvent(ev);
81 }
82
83 /**
84 * Handles the touch event and determines whether to show the fast scroller (or updates it if
85 * it is already showing).
86 */
87 private boolean handleTouchEvent(MotionEvent ev) {
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070088 // Move to mScrollbar's coordinate system.
89 int left = getLeft() - mScrollbar.getLeft();
90 int top = getTop() - mScrollbar.getTop();
91 ev.offsetLocation(left, top);
92 try {
93 return mScrollbar.handleTouchEvent(ev);
94 } finally {
95 ev.offsetLocation(-left, -top);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070096 }
Winson Chung94804152015-05-08 13:06:44 -070097 }
98
99 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
100 // DO NOT REMOVE, NEEDED IMPLEMENTATION FOR M BUILDS
101 }
102
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700103 public int getScrollBarTop() {
104 return getPaddingTop();
105 }
106
Winson Chung94804152015-05-08 13:06:44 -0700107 /**
Sunny Goyal00e10682016-10-18 14:23:46 +0100108 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -0700109 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700110 public int getScrollbarTrackHeight() {
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700111 return getHeight() - getScrollBarTop() - getPaddingBottom();
Winsonc0880492015-08-21 11:16:27 -0700112 }
113
114 /**
Winson Chungb1777442015-06-16 13:35:04 -0700115 * Returns the available scroll height:
116 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -0700117 */
Winsonb655b882016-07-11 18:59:18 -0700118 protected abstract int getAvailableScrollHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700119
120 /**
121 * Returns the available scroll bar height:
122 * AvailableScrollBarHeight = Total height of the visible view - thumb height
123 */
124 protected int getAvailableScrollBarHeight() {
Sunny Goyal00e10682016-10-18 14:23:46 +0100125 int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700126 return availableScrollBarHeight;
127 }
128
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700129 @Override
130 protected void dispatchDraw(Canvas canvas) {
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700131 onUpdateScrollbar(0);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700132 super.dispatchDraw(canvas);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700133 }
134
135 /**
Winson Chungb1777442015-06-16 13:35:04 -0700136 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
137 * this by mapping the available scroll area of the recycler view to the available space for the
138 * scroll bar.
139 *
Winsonb655b882016-07-11 18:59:18 -0700140 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700141 */
Winsonb655b882016-07-11 18:59:18 -0700142 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
143 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700144 // Only show the scrollbar if there is height to be scrolled
145 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700146 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700147 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700148 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700149
Winson Chungb1777442015-06-16 13:35:04 -0700150 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
151 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
152 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100153 int scrollBarY =
154 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700155
Winson Chungb1777442015-06-16 13:35:04 -0700156 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700157 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700158 }
159
160 /**
Sunny Goyalf1fbc3f2017-10-10 15:21:15 -0700161 * Returns whether the view itself will handle the touch event or not.
162 * @param ev MotionEvent in {@param eventSource}
163 */
164 public boolean shouldContainerScroll(MotionEvent ev, View eventSource) {
165 int[] point = new int[2];
166 point[0] = (int) ev.getX();
167 point[1] = (int) ev.getY();
168 Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point);
169 // IF the MotionEvent is inside the thumb, container should not be pulled down.
170 if (mScrollbar.shouldBlockIntercept(point[0], point[1])) {
171 return false;
172 }
173
174 // IF scroller is at the very top OR there is no scroll bar because there is probably not
175 // enough items to scroll, THEN it's okay for the container to be pulled down.
176 if (getCurrentScrollY() == 0) {
177 return true;
178 }
179 return false;
180 }
181
182 /**
Winsonc0880492015-08-21 11:16:27 -0700183 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700184 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700185 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700186 return true;
187 }
188
189 /**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700190 * Maps the touch (from 0..1) to the adapter position that should be visible.
191 * <p>Override in each subclass of this base class.
Winsonc0880492015-08-21 11:16:27 -0700192 *
193 * @return the scroll top of this recycler view.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700194 */
Peter Schiller2e22b5d2016-07-12 12:45:10 -0700195 public abstract int getCurrentScrollY();
Winsonc0880492015-08-21 11:16:27 -0700196
197 /**
198 * Maps the touch (from 0..1) to the adapter position that should be visible.
199 * <p>Override in each subclass of this base class.
200 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700201 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700202
203 /**
204 * Updates the bounds for the scrollbar.
205 * <p>Override in each subclass of this base class.
206 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700207 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700208
209 /**
Winson Chungb1777442015-06-16 13:35:04 -0700210 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700211 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700212 public void onFastScrollCompleted() {}
Winson Chung94804152015-05-08 13:06:44 -0700213}