blob: 3ee6e51b8d08633a5b2dc78fb47fb61b51aa1413 [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 Goyal5d9fb0e2016-10-08 17:43:48 -070024import android.view.ViewGroup;
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070025import android.widget.TextView;
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070026
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070027import com.android.launcher3.views.RecyclerViewFastScroller;
Winson Chung94804152015-05-08 13:06:44 -070028
Winson Chungb1777442015-06-16 13:35:04 -070029
Winson Chung94804152015-05-08 13:06:44 -070030/**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070031 * A base {@link RecyclerView}, which does the following:
32 * <ul>
33 * <li> NOT intercept a touch unless the scrolling velocity is below a predefined threshold.
34 * <li> Enable fast scroller.
35 * </ul>
Winson Chung94804152015-05-08 13:06:44 -070036 */
Winson Chungb1777442015-06-16 13:35:04 -070037public abstract class BaseRecyclerView extends RecyclerView
Winson Chung94804152015-05-08 13:06:44 -070038 implements RecyclerView.OnItemTouchListener {
39
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
55 protected void onFinishInflate() {
56 super.onFinishInflate();
57 addOnItemTouchListener(this);
58 }
59
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070060 @Override
61 protected void onAttachedToWindow() {
62 super.onAttachedToWindow();
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070063 ViewGroup parent = (ViewGroup) getParent();
64 mScrollbar = parent.findViewById(R.id.fast_scroller);
65 mScrollbar.setRecyclerView(this, (TextView) parent.findViewById(R.id.fast_scroller_popup));
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -070066 }
67
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070068 /**
69 * We intercept the touch handling only to support fast scrolling when initiated from the
70 * scroll bar. Otherwise, we fall back to the default RecyclerView touch handling.
71 */
Winson Chung94804152015-05-08 13:06:44 -070072 @Override
73 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070074 return handleTouchEvent(ev);
Winson Chung94804152015-05-08 13:06:44 -070075 }
76
77 @Override
78 public void onTouchEvent(RecyclerView rv, MotionEvent ev) {
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070079 handleTouchEvent(ev);
80 }
81
82 /**
83 * Handles the touch event and determines whether to show the fast scroller (or updates it if
84 * it is already showing).
85 */
86 private boolean handleTouchEvent(MotionEvent ev) {
Sunny Goyal89d5c5a2017-06-23 16:12:50 -070087 // Move to mScrollbar's coordinate system.
88 int left = getLeft() - mScrollbar.getLeft();
89 int top = getTop() - mScrollbar.getTop();
90 ev.offsetLocation(left, top);
91 try {
92 return mScrollbar.handleTouchEvent(ev);
93 } finally {
94 ev.offsetLocation(-left, -top);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -070095 }
Winson Chung94804152015-05-08 13:06:44 -070096 }
97
98 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
99 // DO NOT REMOVE, NEEDED IMPLEMENTATION FOR M BUILDS
100 }
101
102 /**
Sunny Goyal00e10682016-10-18 14:23:46 +0100103 * Returns the height of the fast scroll bar
Winsonc0880492015-08-21 11:16:27 -0700104 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700105 public int getScrollbarTrackHeight() {
Sunny Goyaldc19a072017-05-12 08:17:35 -0700106 return getHeight() - getPaddingTop() - getPaddingBottom();
Winsonc0880492015-08-21 11:16:27 -0700107 }
108
109 /**
Winson Chungb1777442015-06-16 13:35:04 -0700110 * Returns the available scroll height:
111 * AvailableScrollHeight = Total height of the all items - last page height
Winson Chungb1777442015-06-16 13:35:04 -0700112 */
Winsonb655b882016-07-11 18:59:18 -0700113 protected abstract int getAvailableScrollHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700114
115 /**
116 * Returns the available scroll bar height:
117 * AvailableScrollBarHeight = Total height of the visible view - thumb height
118 */
119 protected int getAvailableScrollBarHeight() {
Sunny Goyal00e10682016-10-18 14:23:46 +0100120 int availableScrollBarHeight = getScrollbarTrackHeight() - mScrollbar.getThumbHeight();
Winson Chungb1777442015-06-16 13:35:04 -0700121 return availableScrollBarHeight;
122 }
123
124 /**
Winsonc0880492015-08-21 11:16:27 -0700125 * Returns the scrollbar for this recycler view.
126 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700127 public RecyclerViewFastScroller getScrollBar() {
Winsonc0880492015-08-21 11:16:27 -0700128 return mScrollbar;
129 }
130
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700131 @Override
132 protected void dispatchDraw(Canvas canvas) {
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700133 onUpdateScrollbar(0);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700134 super.dispatchDraw(canvas);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700135 }
136
137 /**
Winson Chungb1777442015-06-16 13:35:04 -0700138 * Updates the scrollbar thumb offset to match the visible scroll of the recycler view. It does
139 * this by mapping the available scroll area of the recycler view to the available space for the
140 * scroll bar.
141 *
Winsonb655b882016-07-11 18:59:18 -0700142 * @param scrollY the current scroll y
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700143 */
Winsonb655b882016-07-11 18:59:18 -0700144 protected void synchronizeScrollBarThumbOffsetToViewScroll(int scrollY,
145 int availableScrollHeight) {
Winson Chungb1777442015-06-16 13:35:04 -0700146 // Only show the scrollbar if there is height to be scrolled
147 if (availableScrollHeight <= 0) {
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700148 mScrollbar.setThumbOffsetY(-1);
Winson Chungb1777442015-06-16 13:35:04 -0700149 return;
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700150 }
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700151
Winson Chungb1777442015-06-16 13:35:04 -0700152 // Calculate the current scroll position, the scrollY of the recycler view accounts for the
153 // view padding, while the scrollBarY is drawn right up to the background padding (ignoring
154 // padding)
Sunny Goyal00e10682016-10-18 14:23:46 +0100155 int scrollBarY =
156 (int) (((float) scrollY / availableScrollHeight) * getAvailableScrollBarHeight());
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700157
Winson Chungb1777442015-06-16 13:35:04 -0700158 // Calculate the position and size of the scroll bar
Sunny Goyal5d9fb0e2016-10-08 17:43:48 -0700159 mScrollbar.setThumbOffsetY(scrollBarY);
Winson Chung4b9f9792015-06-12 18:02:52 -0700160 }
161
162 /**
Winsonc0880492015-08-21 11:16:27 -0700163 * @return whether fast scrolling is supported in the current state.
Winson646c2362015-09-03 11:46:11 -0700164 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700165 public boolean supportsFastScrolling() {
Winson646c2362015-09-03 11:46:11 -0700166 return true;
167 }
168
169 /**
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700170 * Maps the touch (from 0..1) to the adapter position that should be visible.
171 * <p>Override in each subclass of this base class.
Winsonc0880492015-08-21 11:16:27 -0700172 *
173 * @return the scroll top of this recycler view.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700174 */
Peter Schiller2e22b5d2016-07-12 12:45:10 -0700175 public abstract int getCurrentScrollY();
Winsonc0880492015-08-21 11:16:27 -0700176
177 /**
178 * Maps the touch (from 0..1) to the adapter position that should be visible.
179 * <p>Override in each subclass of this base class.
180 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700181 public abstract String scrollToPositionAtProgress(float touchFraction);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700182
183 /**
184 * Updates the bounds for the scrollbar.
185 * <p>Override in each subclass of this base class.
186 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700187 public abstract void onUpdateScrollbar(int dy);
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700188
189 /**
Winson Chungb1777442015-06-16 13:35:04 -0700190 * <p>Override in each subclass of this base class.
Hyunyoung Songac5f6af2015-05-29 12:00:44 -0700191 */
Sunny Goyal89d5c5a2017-06-23 16:12:50 -0700192 public void onFastScrollCompleted() {}
Winson Chung94804152015-05-08 13:06:44 -0700193}