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 | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 24 | import android.view.View; |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 25 | import android.view.ViewGroup; |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 26 | import android.widget.TextView; |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 27 | |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 28 | import com.android.launcher3.views.RecyclerViewFastScroller; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 29 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 30 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 31 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 32 | * 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 Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 37 | */ |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 38 | public abstract class BaseRecyclerView extends RecyclerView |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 39 | implements RecyclerView.OnItemTouchListener { |
| 40 | |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 41 | protected RecyclerViewFastScroller mScrollbar; |
Sunny Goyal | 9e4c359 | 2017-06-07 14:05:08 -0700 | [diff] [blame] | 42 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 43 | public BaseRecyclerView(Context context) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 44 | this(context, null); |
| 45 | } |
| 46 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 47 | public BaseRecyclerView(Context context, AttributeSet attrs) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 48 | this(context, attrs, 0); |
| 49 | } |
| 50 | |
Winson Chung | 5f4e0fd | 2015-05-22 11:12:27 -0700 | [diff] [blame] | 51 | public BaseRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 52 | super(context, attrs, defStyleAttr); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 55 | @Override |
| 56 | protected void onFinishInflate() { |
| 57 | super.onFinishInflate(); |
| 58 | addOnItemTouchListener(this); |
| 59 | } |
| 60 | |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 61 | @Override |
| 62 | protected void onAttachedToWindow() { |
| 63 | super.onAttachedToWindow(); |
Mario Bertschler | ac9408a | 2017-10-18 10:31:36 -0700 | [diff] [blame] | 64 | bindFastScrollbar(); |
| 65 | } |
| 66 | |
| 67 | public void bindFastScrollbar() { |
| 68 | ViewGroup parent = (ViewGroup) getParent().getParent(); |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 69 | mScrollbar = parent.findViewById(R.id.fast_scroller); |
Mario Bertschler | ac9408a | 2017-10-18 10:31:36 -0700 | [diff] [blame] | 70 | mScrollbar.setRecyclerView(this, parent.findViewById(R.id.fast_scroller_popup)); |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 73 | /** |
| 74 | * We intercept the touch handling only to support fast scrolling when initiated from the |
| 75 | * scroll bar. Otherwise, we fall back to the default RecyclerView touch handling. |
| 76 | */ |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) { |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 79 | return handleTouchEvent(ev); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onTouchEvent(RecyclerView rv, MotionEvent ev) { |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 84 | handleTouchEvent(ev); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Handles the touch event and determines whether to show the fast scroller (or updates it if |
| 89 | * it is already showing). |
| 90 | */ |
| 91 | private boolean handleTouchEvent(MotionEvent ev) { |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 92 | // Move to mScrollbar's coordinate system. |
| 93 | int left = getLeft() - mScrollbar.getLeft(); |
| 94 | int top = getTop() - mScrollbar.getTop(); |
| 95 | ev.offsetLocation(left, top); |
| 96 | try { |
| 97 | return mScrollbar.handleTouchEvent(ev); |
| 98 | } finally { |
| 99 | ev.offsetLocation(-left, -top); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 100 | } |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 104 | // DO NOT REMOVE, NEEDED IMPLEMENTATION FOR M BUILDS |
| 105 | } |
| 106 | |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 107 | public int getScrollBarTop() { |
| 108 | return getPaddingTop(); |
| 109 | } |
| 110 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 111 | /** |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 112 | * Returns the height of the fast scroll bar |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 113 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 114 | public int getScrollbarTrackHeight() { |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 115 | return getHeight() - getScrollBarTop() - getPaddingBottom(); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 119 | * Returns the available scroll height: |
| 120 | * AvailableScrollHeight = Total height of the all items - last page height |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 121 | */ |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 122 | protected abstract int getAvailableScrollHeight(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Returns the available scroll bar height: |
| 126 | * AvailableScrollBarHeight = Total height of the visible view - thumb height |
| 127 | */ |
| 128 | protected int getAvailableScrollBarHeight() { |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 129 | int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight(); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 130 | return availableScrollBarHeight; |
| 131 | } |
| 132 | |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 133 | @Override |
| 134 | protected void dispatchDraw(Canvas canvas) { |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 135 | onUpdateScrollbar(0); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 136 | super.dispatchDraw(canvas); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 140 | * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does |
| 141 | * this by mapping the available scroll area of the recycler view to the available space for the |
| 142 | * scroll bar. |
| 143 | * |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 144 | * @param scrollY the current scroll y |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 145 | */ |
Winson | b655b88 | 2016-07-11 18:59:18 -0700 | [diff] [blame] | 146 | protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY, |
| 147 | int availableScrollHeight) { |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 148 | // Only show the scrollbar if there is height to be scrolled |
| 149 | if (availableScrollHeight <= 0) { |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 150 | mScrollbar.setThumbOffsetY(-1); |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 151 | return; |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 152 | } |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 153 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 154 | // Calculate the current scroll position, the scrollY of the recycler view accounts for the |
| 155 | // view padding, while the scrollBarY is drawn right up to the background padding (ignoring |
| 156 | // padding) |
Sunny Goyal | 00e1068 | 2016-10-18 14:23:46 +0100 | [diff] [blame] | 157 | int scrollBarY = |
| 158 | (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight()); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 159 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 160 | // Calculate the position and size of the scroll bar |
Sunny Goyal | 5d9fb0e | 2016-10-08 17:43:48 -0700 | [diff] [blame] | 161 | mScrollbar.setThumbOffsetY(scrollBarY); |
Winson Chung | 4b9f979 | 2015-06-12 18:02:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
Sunny Goyal | f1fbc3f | 2017-10-10 15:21:15 -0700 | [diff] [blame] | 165 | * Returns whether the view itself will handle the touch event or not. |
| 166 | * @param ev MotionEvent in {@param eventSource} |
| 167 | */ |
| 168 | public boolean shouldContainerScroll(MotionEvent ev, View eventSource) { |
| 169 | int[] point = new int[2]; |
| 170 | point[0] = (int) ev.getX(); |
| 171 | point[1] = (int) ev.getY(); |
| 172 | Utilities.mapCoordInSelfToDescendant(mScrollbar, eventSource, point); |
| 173 | // IF the MotionEvent is inside the thumb, container should not be pulled down. |
| 174 | if (mScrollbar.shouldBlockIntercept(point[0], point[1])) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // IF scroller is at the very top OR there is no scroll bar because there is probably not |
| 179 | // enough items to scroll, THEN it's okay for the container to be pulled down. |
| 180 | if (getCurrentScrollY() == 0) { |
| 181 | return true; |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /** |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 187 | * @return whether fast scrolling is supported in the current state. |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame] | 188 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 189 | public boolean supportsFastScrolling() { |
Winson | 646c236 | 2015-09-03 11:46:11 -0700 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /** |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 194 | * Maps the touch (from 0..1) to the adapter position that should be visible. |
| 195 | * <p>Override in each subclass of this base class. |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 196 | * |
| 197 | * @return the scroll top of this recycler view. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 198 | */ |
Peter Schiller | 2e22b5d | 2016-07-12 12:45:10 -0700 | [diff] [blame] | 199 | public abstract int getCurrentScrollY(); |
Winson | c088049 | 2015-08-21 11:16:27 -0700 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Maps the touch (from 0..1) to the adapter position that should be visible. |
| 203 | * <p>Override in each subclass of this base class. |
| 204 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 205 | public abstract String scrollToPositionAtProgress(float touchFraction); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Updates the bounds for the scrollbar. |
| 209 | * <p>Override in each subclass of this base class. |
| 210 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 211 | public abstract void onUpdateScrollbar(int dy); |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 212 | |
| 213 | /** |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 214 | * <p>Override in each subclass of this base class. |
Hyunyoung Song | ac5f6af | 2015-05-29 12:00:44 -0700 | [diff] [blame] | 215 | */ |
Sunny Goyal | 89d5c5a | 2017-06-23 16:12:50 -0700 | [diff] [blame] | 216 | public void onFastScrollCompleted() {} |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 217 | } |