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