Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.content.Context; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 21 | import android.graphics.Rect; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 22 | import android.support.v7.widget.RecyclerView; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.view.MotionEvent; |
| 25 | import com.android.launcher3.util.Thunk; |
| 26 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 27 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 28 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 29 | * A base {@link RecyclerView}, which does the following: |
| 30 | * <ul> |
| 31 | * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold. |
| 32 | * <li> Enable fast scroller. |
| 33 | * </ul> |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 34 | */ |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 35 | public abstract class BaseRecyclerView extends RecyclerView |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 36 | implements RecyclerView.OnItemTouchListener { |
| 37 | |
| 38 | private static final int SCROLL_DELTA_THRESHOLD_DP = 4; |
| 39 | |
| 40 | /** Keeps the last known scrolling delta/velocity along y-axis. */ |
| 41 | @Thunk int mDy = 0; |
| 42 | private float mDeltaThreshold; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 43 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 44 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 45 | * The current scroll state of the recycler view. We use this in onUpdateScrollbar() |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 46 | * and scrollToPositionAtProgress() to determine the scroll position of the recycler view so |
| 47 | * that we can calculate what the scroll bar looks like, and where to jump to from the fast |
| 48 | * scroller. |
| 49 | */ |
| 50 | public static class ScrollPositionState { |
| 51 | // The index of the first visible row |
| 52 | public int rowIndex; |
| 53 | // The offset of the first visible row |
| 54 | public int rowTopOffset; |
| 55 | // The height of a given row (they are currently all the same height) |
| 56 | public int rowHeight; |
| 57 | } |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 58 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 59 | protected BaseRecyclerViewFastScrollBar mScrollbar; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 60 | |
| 61 | private int mDownX; |
| 62 | private int mDownY; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 63 | private int mLastY; |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 64 | protected Rect mBackgroundPadding = new Rect(); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 65 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 66 | public BaseRecyclerView(Context context) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 67 | this(context, null); |
| 68 | } |
| 69 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 70 | public BaseRecyclerView(Context context, AttributeSet attrs) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 71 | this(context, attrs, 0); |
| 72 | } |
| 73 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 74 | public BaseRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 75 | super(context, attrs, defStyleAttr); |
| 76 | mDeltaThreshold = getResources().getDisplayMetrics().density * SCROLL_DELTA_THRESHOLD_DP; |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 77 | mScrollbar = new BaseRecyclerViewFastScrollBar(this, getResources()); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 78 | |
| 79 | ScrollListener listener = new ScrollListener(); |
| 80 | setOnScrollListener(listener); |
| 81 | } |
| 82 | |
| 83 | private class ScrollListener extends OnScrollListener { |
| 84 | public ScrollListener() { |
| 85 | // Do nothing |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onScrolled(RecyclerView recyclerView, int dx, int dy) { |
| 90 | mDy = dy; |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 91 | |
| 92 | // TODO(winsonc): If we want to animate the section heads while scrolling, we can |
| 93 | // initiate that here if the recycler view scroll state is not |
| 94 | // RecyclerView.SCROLL_STATE_IDLE. |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 95 | |
| 96 | onUpdateScrollbar(dy); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 100 | public void reset() { |
| 101 | mScrollbar.reattachThumbToScroll(); |
| 102 | } |
| 103 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 104 | @Override |
| 105 | protected void onFinishInflate() { |
| 106 | super.onFinishInflate(); |
| 107 | addOnItemTouchListener(this); |
| 108 | } |
| 109 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 110 | /** |
| 111 | * We intercept the touch handling only to support fast scrolling when initiated from the |
| 112 | * scroll bar. Otherwise, we fall back to the default RecyclerView touch handling. |
| 113 | */ |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) { |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 116 | return handleTouchEvent(ev); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onTouchEvent(RecyclerView rv, MotionEvent ev) { |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 121 | handleTouchEvent(ev); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Handles the touch event and determines whether to show the fast scroller (or updates it if |
| 126 | * it is already showing). |
| 127 | */ |
| 128 | private boolean handleTouchEvent(MotionEvent ev) { |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 129 | int action = ev.getAction(); |
| 130 | int x = (int) ev.getX(); |
| 131 | int y = (int) ev.getY(); |
| 132 | switch (action) { |
| 133 | case MotionEvent.ACTION_DOWN: |
| 134 | // Keep track of the down positions |
Hyunyoung Song | ec84728 | 2015-06-04 11:37:46 -0700 | [diff] [blame] | 135 | mDownX = x; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 136 | mDownY = mLastY = y; |
| 137 | if (shouldStopScroll(ev)) { |
| 138 | stopScroll(); |
| 139 | } |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 140 | mScrollbar.handleTouchEvent(ev, mDownX, mDownY, mLastY); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 141 | break; |
| 142 | case MotionEvent.ACTION_MOVE: |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 143 | mLastY = y; |
| 144 | mScrollbar.handleTouchEvent(ev, mDownX, mDownY, mLastY); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 145 | break; |
| 146 | case MotionEvent.ACTION_UP: |
| 147 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 148 | onFastScrollCompleted(); |
| 149 | mScrollbar.handleTouchEvent(ev, mDownX, mDownY, mLastY); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 150 | break; |
| 151 | } |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 152 | return mScrollbar.isDraggingThumb(); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 156 | // DO NOT REMOVE, NEEDED IMPLEMENTATION FOR M BUILDS |
| 157 | } |
| 158 | |
| 159 | /** |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 160 | * Returns whether this {@link MotionEvent} should trigger the scroll to be stopped. |
| 161 | */ |
| 162 | protected boolean shouldStopScroll(MotionEvent ev) { |
| 163 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 164 | if ((Math.abs(mDy) < mDeltaThreshold && |
| 165 | getScrollState() != RecyclerView.SCROLL_STATE_IDLE)) { |
| 166 | // now the touch events are being passed to the {@link WidgetCell} until the |
| 167 | // touch sequence goes over the touch slop. |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | return false; |
| 172 | } |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 173 | |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 174 | public void updateBackgroundPadding(Rect padding) { |
| 175 | mBackgroundPadding.set(padding); |
| 176 | } |
| 177 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 178 | public Rect getBackgroundPadding() { |
| 179 | return mBackgroundPadding; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns the scroll bar width when the user is scrolling. |
| 184 | */ |
| 185 | public int getMaxScrollbarWidth() { |
| 186 | return mScrollbar.getThumbMaxWidth(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the available scroll height: |
| 191 | * AvailableScrollHeight = Total height of the all items - last page height |
| 192 | * |
| 193 | * This assumes that all rows are the same height. |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 194 | */ |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 195 | protected int getAvailableScrollHeight(int rowCount, int rowHeight) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 196 | int visibleHeight = getHeight() - mBackgroundPadding.top - mBackgroundPadding.bottom; |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 197 | int scrollHeight = getPaddingTop() + rowCount * rowHeight + getPaddingBottom(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 198 | int availableScrollHeight = scrollHeight - visibleHeight; |
| 199 | return availableScrollHeight; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns the available scroll bar height: |
| 204 | * AvailableScrollBarHeight = Total height of the visible view - thumb height |
| 205 | */ |
| 206 | protected int getAvailableScrollBarHeight() { |
| 207 | int visibleHeight = getHeight() - mBackgroundPadding.top - mBackgroundPadding.bottom; |
| 208 | int availableScrollBarHeight = visibleHeight - mScrollbar.getThumbHeight(); |
| 209 | return availableScrollBarHeight; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns the track color (ignoring alpha), can be overridden by each subclass. |
| 214 | */ |
| 215 | public int getFastScrollerTrackColor(int defaultTrackColor) { |
| 216 | return defaultTrackColor; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the inactive thumb color, can be overridden by each subclass. |
| 221 | */ |
| 222 | public int getFastScrollerThumbInactiveColor(int defaultInactiveThumbColor) { |
| 223 | return defaultInactiveThumbColor; |
| 224 | } |
| 225 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 226 | @Override |
| 227 | protected void dispatchDraw(Canvas canvas) { |
| 228 | super.dispatchDraw(canvas); |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 229 | onUpdateScrollbar(0); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 230 | mScrollbar.draw(canvas); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 234 | * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does |
| 235 | * this by mapping the available scroll area of the recycler view to the available space for the |
| 236 | * scroll bar. |
| 237 | * |
| 238 | * @param scrollPosState the current scroll position |
| 239 | * @param rowCount the number of rows, used to calculate the total scroll height (assumes that |
| 240 | * all rows are the same height) |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 241 | */ |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 242 | protected void synchronizeScrollBarThumbOffsetToViewScroll(ScrollPositionState scrollPosState, |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 243 | int rowCount) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 244 | // Only show the scrollbar if there is height to be scrolled |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 245 | int availableScrollBarHeight = getAvailableScrollBarHeight(); |
| 246 | int availableScrollHeight = getAvailableScrollHeight(rowCount, scrollPosState.rowHeight); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 247 | if (availableScrollHeight <= 0) { |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 248 | mScrollbar.setThumbOffset(-1, -1); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 249 | return; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 250 | } |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 251 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 252 | // Calculate the current scroll position, the scrollY of the recycler view accounts for the |
| 253 | // view padding, while the scrollBarY is drawn right up to the background padding (ignoring |
| 254 | // padding) |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 255 | int scrollY = getPaddingTop() + |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 256 | (scrollPosState.rowIndex * scrollPosState.rowHeight) - scrollPosState.rowTopOffset; |
| 257 | int scrollBarY = mBackgroundPadding.top + |
| 258 | (int) (((float) scrollY / availableScrollHeight) * availableScrollBarHeight); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 259 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 260 | // Calculate the position and size of the scroll bar |
| 261 | int scrollBarX; |
| 262 | if (Utilities.isRtl(getResources())) { |
| 263 | scrollBarX = mBackgroundPadding.left; |
| 264 | } else { |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 265 | scrollBarX = getWidth() - mBackgroundPadding.right - mScrollbar.getThumbWidth(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 266 | } |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 267 | mScrollbar.setThumbOffset(scrollBarX, scrollBarY); |
Winson Chung | 4b9f979 | 2015-06-12 18:02:52 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame^] | 271 | * Returns whether fast scrolling is supported in the current state. |
| 272 | */ |
| 273 | protected boolean supportsFastScrolling() { |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 278 | * Maps the touch (from 0..1) to the adapter position that should be visible. |
| 279 | * <p>Override in each subclass of this base class. |
| 280 | */ |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 281 | public abstract String scrollToPositionAtProgress(float touchFraction); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 282 | |
| 283 | /** |
| 284 | * Updates the bounds for the scrollbar. |
| 285 | * <p>Override in each subclass of this base class. |
| 286 | */ |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 287 | public abstract void onUpdateScrollbar(int dy); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 290 | * <p>Override in each subclass of this base class. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 291 | */ |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 292 | public void onFastScrollCompleted() {} |
Winson | d2eb49e | 2015-08-18 17:43:02 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * Returns information about the item that the recycler view is currently scrolled to. |
| 296 | */ |
| 297 | protected abstract void getCurScrollState(ScrollPositionState stateOut); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 298 | } |