-
Notifications
You must be signed in to change notification settings - Fork 106
/
FastScroller.java
292 lines (255 loc) · 11.2 KB
/
FastScroller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.futuremind.recyclerviewfastscroll;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.futuremind.recyclerviewfastscroll.viewprovider.DefaultScrollerViewProvider;
import com.futuremind.recyclerviewfastscroll.viewprovider.ScrollerViewProvider;
/**
* Created by mklimczak on 28/07/15.
*/
public class FastScroller extends LinearLayout {
private static final int STYLE_NONE = -1;
private final RecyclerViewScrollListener scrollListener = new RecyclerViewScrollListener(this);
private RecyclerView recyclerView;
private View bubble;
private View handle;
private TextView bubbleTextView;
private int bubbleOffset;
private int handleColor;
private int bubbleColor;
private int bubbleTextAppearance;
private int scrollerOrientation;
//TODO the name should be fixed, also check if there is a better way of handling the visibility, because this is somewhat convoluted
private int maxVisibility;
private boolean manuallyChangingPosition;
private ScrollerViewProvider viewProvider;
private SectionTitleProvider titleProvider;
public FastScroller(Context context) {
this(context, null);
}
public FastScroller(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FastScroller(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setClipChildren(false);
TypedArray style = context.obtainStyledAttributes(attrs, R.styleable.fastscroll__fastScroller, R.attr.fastscroll__style, 0);
try {
bubbleColor = style.getColor(R.styleable.fastscroll__fastScroller_fastscroll__bubbleColor, STYLE_NONE);
handleColor = style.getColor(R.styleable.fastscroll__fastScroller_fastscroll__handleColor, STYLE_NONE);
bubbleTextAppearance = style.getResourceId(R.styleable.fastscroll__fastScroller_fastscroll__bubbleTextAppearance, STYLE_NONE);
} finally {
style.recycle();
}
maxVisibility = getVisibility();
setViewProvider(new DefaultScrollerViewProvider());
}
/**
* Enables custom layout for {@link FastScroller}.
* @param viewProvider A {@link ScrollerViewProvider} for the {@link FastScroller} to use when building layout.
*/
public void setViewProvider(ScrollerViewProvider viewProvider) {
removeAllViews();
this.viewProvider = viewProvider;
viewProvider.setFastScroller(this);
bubble = viewProvider.provideBubbleView(this);
handle = viewProvider.provideHandleView(this);
bubbleTextView = viewProvider.provideBubbleTextView();
addView(bubble);
addView(handle);
}
/**
* Attach the {@link FastScroller} to {@link RecyclerView}. Should be used after the adapter is set
* to the {@link RecyclerView}. If the adapter implements SectionTitleProvider, the FastScroller
* will show a bubble with title.
* @param recyclerView A {@link RecyclerView} to attach the {@link FastScroller} to.
*/
public void setRecyclerView(RecyclerView recyclerView) {
this.recyclerView = recyclerView;
if(recyclerView.getAdapter() instanceof SectionTitleProvider) titleProvider = (SectionTitleProvider) recyclerView.getAdapter();
recyclerView.addOnScrollListener(scrollListener);
invalidateVisibility();
recyclerView.setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
invalidateVisibility();
}
@Override
public void onChildViewRemoved(View parent, View child) {
invalidateVisibility();
}
});
}
/**
* Set the orientation of the {@link FastScroller}. The orientation of the {@link FastScroller}
* should generally match the orientation of connected {@link RecyclerView} for good UX but it's not enforced.
* Note: This method is overridden from {@link LinearLayout#setOrientation(int)} but for {@link FastScroller}
* it has a totally different meaning.
* @param orientation of the {@link FastScroller}. {@link #VERTICAL} or {@link #HORIZONTAL}
*/
@Override
public void setOrientation(int orientation) {
scrollerOrientation = orientation;
//switching orientation, because orientation in linear layout
//is something different than orientation of fast scroller
super.setOrientation(orientation == HORIZONTAL ? VERTICAL : HORIZONTAL);
}
/**
* Set the background color of the bubble.
* @param color Color in hex notation with alpha channel, e.g. 0xFFFFFFFF
*/
public void setBubbleColor(int color) {
bubbleColor = color;
invalidate();
}
/**
* Set the background color of the handle.
* @param color Color in hex notation with alpha channel, e.g. 0xFFFFFFFF
*/
public void setHandleColor(int color) {
handleColor = color;
invalidate();
}
/**
* Sets the text appearance of the bubble.
* @param textAppearanceResourceId The id of the resource to be used as text appearance of the bubble.
*/
public void setBubbleTextAppearance(int textAppearanceResourceId){
bubbleTextAppearance = textAppearanceResourceId;
invalidate();
}
/**
* Add a {@link com.futuremind.recyclerviewfastscroll.RecyclerViewScrollListener.ScrollerListener}
* to be notified of user scrolling
* @param listener
*/
public void addScrollerListener(RecyclerViewScrollListener.ScrollerListener listener){
scrollListener.addScrollerListener(listener);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
initHandleMovement();
bubbleOffset = viewProvider.getBubbleOffset();
applyStyling(); //TODO this doesn't belong here, even if it works
if (!isInEditMode()) {
//sometimes recycler starts with a defined scroll (e.g. when coming from saved state)
scrollListener.updateHandlePosition(recyclerView);
}
}
private void applyStyling() {
if(bubbleColor!=STYLE_NONE) setBackgroundTint(bubbleTextView, bubbleColor);
if(handleColor!=STYLE_NONE) setBackgroundTint(handle, handleColor);
if(bubbleTextAppearance!=STYLE_NONE) TextViewCompat.setTextAppearance(bubbleTextView, bubbleTextAppearance);
}
private void setBackgroundTint(View view, int color) {
final Drawable background = DrawableCompat.wrap(view.getBackground());
if(background==null) return;
DrawableCompat.setTint(background.mutate(), color);
Utils.setBackground(view, background);
}
private void initHandleMovement() {
handle.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
requestDisallowInterceptTouchEvent(true);
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
if(titleProvider!=null && event.getAction() == MotionEvent.ACTION_DOWN) viewProvider.onHandleGrabbed();
manuallyChangingPosition = true;
float relativePos = getRelativeTouchPosition(event);
setScrollerPosition(relativePos);
setRecyclerViewPosition(relativePos);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
manuallyChangingPosition = false;
if(titleProvider!=null) viewProvider.onHandleReleased();
return true;
}
return false;
}
});
}
private float getRelativeTouchPosition(MotionEvent event){
if(isVertical()){
float yInParent = event.getRawY() - Utils.getViewRawY(handle);
return yInParent / (getHeight() - handle.getHeight());
} else {
float xInParent = event.getRawX() - Utils.getViewRawX(handle);
return xInParent / (getWidth() - handle.getWidth());
}
}
@Override
public void setVisibility(int visibility) {
maxVisibility = visibility;
invalidateVisibility();
}
private void invalidateVisibility() {
if(
recyclerView.getAdapter()==null ||
recyclerView.getAdapter().getItemCount()==0 ||
recyclerView.getChildAt(0)==null ||
isRecyclerViewNotScrollable() ||
maxVisibility != View.VISIBLE
){
super.setVisibility(INVISIBLE);
} else {
super.setVisibility(VISIBLE);
}
}
private boolean isRecyclerViewNotScrollable() {
if(isVertical()) {
return recyclerView.getChildAt(0).getHeight() * recyclerView.getAdapter().getItemCount() <= recyclerView.getHeight();
} else {
return recyclerView.getChildAt(0).getWidth() * recyclerView.getAdapter().getItemCount() <= recyclerView.getWidth();
}
}
private void setRecyclerViewPosition(float relativePos) {
if (recyclerView == null) return;
int itemCount = recyclerView.getAdapter().getItemCount();
int targetPos = (int) Utils.getValueInRange(0, itemCount - 1, (int) (relativePos * (float) itemCount));
recyclerView.scrollToPosition(targetPos);
if(titleProvider!=null && bubbleTextView!=null) bubbleTextView.setText(titleProvider.getSectionTitle(targetPos));
}
void setScrollerPosition(float relativePos) {
if(isVertical()) {
bubble.setY(Utils.getValueInRange(
0,
getHeight() - bubble.getHeight(),
relativePos * (getHeight() - handle.getHeight()) + bubbleOffset)
);
handle.setY(Utils.getValueInRange(
0,
getHeight() - handle.getHeight(),
relativePos * (getHeight() - handle.getHeight()))
);
} else {
bubble.setX(Utils.getValueInRange(
0,
getWidth() - bubble.getWidth(),
relativePos * (getWidth() - handle.getWidth()) + bubbleOffset)
);
handle.setX(Utils.getValueInRange(
0,
getWidth() - handle.getWidth(),
relativePos * (getWidth() - handle.getWidth()))
);
}
}
public boolean isVertical(){
return scrollerOrientation == VERTICAL;
}
boolean shouldUpdateHandlePosition() {
return handle!=null && !manuallyChangingPosition && recyclerView.getChildCount() > 0;
}
ScrollerViewProvider getViewProvider() {
return viewProvider;
}
}