/
UltraRecyclerView.java
433 lines (398 loc) · 15 KB
/
UltraRecyclerView.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.jimmysun.ultrarecyclerview;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.annotation.Px;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.animation.Interpolator;
import android.widget.OverScroller;
import java.lang.reflect.Field;
/**
* The Ultra RecyclerView
*
* @author SunQiang
* @since 2019-04-16
*/
public class UltraRecyclerView extends RecyclerView {
private static final int AUTO_SCROLL_DURATION = 5000;
private UltraRecyclerViewAdapter mRecyclerViewAdapter;
// pager snap
private GravityPagerSnapHelper mPagerSnapHelper;
private UltraLinearSmoothScroller mSmoothScroller;
private int mAlignGravity = Gravity.NO_GRAVITY;
private int mAlignMargin;
// infinite loop
private boolean mInfiniteLoop;
// auto scroll
private int mAutoScrollStartDelay;
private int mAutoScrollDuration;
private boolean mStopAutoScroll;
private Runnable mScrollRunnable;
private int mAutoScrollSpeed;
private Interpolator mInterpolator;
public UltraRecyclerView(Context context) {
super(context);
init(context);
setOrientation(HORIZONTAL);
}
public UltraRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
init(context, attrs);
}
public UltraRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
init(context, attrs);
}
private void init(Context context) {
mSmoothScroller = new UltraLinearSmoothScroller(context);
mInterpolator = new Interpolator() {
@Override
public float getInterpolation(float t) {
--t;
return t * t * t * t * t + 1.0F;
}
};
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.UltraRecyclerView);
setOrientation(typedArray.getInt(R.styleable.UltraRecyclerView_orientation, HORIZONTAL));
int gravity = typedArray.getInt(R.styleable.UltraRecyclerView_alignGravity,
Gravity.NO_GRAVITY);
int margin = typedArray.getDimensionPixelSize(R.styleable.UltraRecyclerView_alignMargin, 0);
setPagerSnap(gravity, margin);
setInfiniteLoop(typedArray.getBoolean(R.styleable.UltraRecyclerView_infiniteLoop, false));
typedArray.recycle();
}
/**
* Sets the scroll orientation.
*
* @param orientation One of {@link #HORIZONTAL}, or {@link #VERTICAL}.
*/
public void setOrientation(int orientation) {
switch (orientation) {
case HORIZONTAL:
setLayoutManager(new LinearLayoutManager(getContext(),
LinearLayoutManager.HORIZONTAL, false));
break;
case VERTICAL:
setLayoutManager(new LinearLayoutManager(getContext(),
LinearLayoutManager.VERTICAL, false));
break;
default:
}
}
@Override
public void setAdapter(Adapter adapter) {
mRecyclerViewAdapter = buildUltraRecyclerViewAdapter(adapter);
mRecyclerViewAdapter.setInfiniteLoop(mInfiniteLoop);
super.setAdapter(mRecyclerViewAdapter);
}
@Override
public void swapAdapter(Adapter adapter, boolean removeAndRecycleExistingViews) {
mRecyclerViewAdapter = buildUltraRecyclerViewAdapter(adapter);
super.swapAdapter(mRecyclerViewAdapter, removeAndRecycleExistingViews);
}
private UltraRecyclerViewAdapter buildUltraRecyclerViewAdapter(Adapter<?> adapter) {
if (adapter instanceof UltraRecyclerViewAdapter) {
return (UltraRecyclerViewAdapter) adapter;
} else {
return new UltraRecyclerViewAdapter<>(adapter);
}
}
/**
* Returns whether this view enables pager snap.
*
* @return True if this view enables pager snap, or false otherwise.
*/
public boolean isPagerSnap() {
return mAlignGravity == Gravity.START || mAlignGravity == Gravity.CENTER
|| mAlignGravity == Gravity.END;
}
/**
* Enables or disables pager snap and sets align gravity.
*
* @param gravity The align gravity. Should be one of {@link Gravity#CENTER},
* {@link Gravity#START}, {@link Gravity#END}, or {@link Gravity#NO_GRAVITY}
* to disable pager snap.
*/
public void setPagerSnap(int gravity) {
setPagerSnap(gravity, 0);
}
/**
* Enables or disables pager snap, and sets align gravity and margin.
*
* @param gravity The align gravity. Should be one of {@link Gravity#CENTER},
* {@link Gravity#START}, {@link Gravity#END}, or {@link Gravity#NO_GRAVITY}.
* @param alignMargin The align margin in px, only valid when the gravity is
* {@link Gravity#START} or {@link Gravity#END}.
*/
public void setPagerSnap(int gravity, @Px int alignMargin) {
mAlignGravity = gravity;
mAlignMargin = alignMargin;
if (mPagerSnapHelper != null) {
mPagerSnapHelper.attachToRecyclerView(null);
}
if (isPagerSnap()) {
mPagerSnapHelper = new GravityPagerSnapHelper(mAlignGravity);
mPagerSnapHelper.setAlignMargin(mAlignMargin);
mPagerSnapHelper.attachToRecyclerView(this);
mSmoothScroller.setGravity(mAlignGravity);
mSmoothScroller.setMargin(mAlignMargin);
}
}
/**
* Register a callback to be invoked when this view enables pager snap and changes position.
*
* @param onSnapListener The callback that will run.
*/
public void setOnSnapListener(GravityPagerSnapHelper.OnSnapListener onSnapListener) {
if (mPagerSnapHelper != null) {
mPagerSnapHelper.setOnSnapListener(onSnapListener);
}
}
/**
* Returns whether the items are infinite loop.
*
* @return True if the items are infinite loop, or false otherwise.
*/
public boolean isInfiniteLoop() {
return mRecyclerViewAdapter != null && mRecyclerViewAdapter.isInfiniteLoop();
}
/**
* Enables or disables the items infinite loop.
*
* @param infiniteLoop True to enable the items infinite loop, or false otherwise.
*/
public void setInfiniteLoop(boolean infiniteLoop) {
mInfiniteLoop = infiniteLoop;
if (mRecyclerViewAdapter != null) {
mRecyclerViewAdapter.setInfiniteLoop(infiniteLoop);
}
}
/**
* Starts auto scrolling, and the duration is default.
*/
public void startAutoScroll() {
startAutoScroll(AUTO_SCROLL_DURATION);
}
/**
* Starts auto scrolling, and the duration is specified.
*
* @param duration The duration in milliseconds.
*/
public void startAutoScroll(int duration) {
startAutoScrollDelayed(0, duration);
}
/**
* Starts auto scrolling, the start delay-time is specified, and the duration is default.
*
* @param delay The start delay-time in milliseconds.
*/
public void startAutoScrollDelayed(int delay) {
startAutoScrollDelayed(delay, AUTO_SCROLL_DURATION);
}
/**
* Starts auto scrolling, the duration and start delay-time are specified.
*
* @param delay The duration in milliseconds.
* @param duration The start delay-time in milliseconds.
*/
public void startAutoScrollDelayed(int delay, int duration) {
stopAutoScroll();
if (mRecyclerViewAdapter != null && mRecyclerViewAdapter.getItemCount() > 1) {
mAutoScrollStartDelay = delay < 0 ? 0 : delay;
mAutoScrollDuration = duration < 0 ? AUTO_SCROLL_DURATION : duration;
mScrollRunnable = new Runnable() {
@Override
public void run() {
if (!mStopAutoScroll && mRecyclerViewAdapter != null
&& mRecyclerViewAdapter.getItemCount() > 0 && getLayoutManager() != null) {
mSmoothScroller.setTargetPosition((getCurrentPosition() + 1) % mRecyclerViewAdapter.getItemCount());
if (mAutoScrollSpeed > 0) {
setScrollSpeed(mAutoScrollSpeed, mInterpolator);
} else {
setScrollSpeed(-1, mInterpolator);
}
getLayoutManager().startSmoothScroll(mSmoothScroller);
removeCallbacks(this);
postDelayed(this, mAutoScrollDuration);
}
}
};
postDelayed(mScrollRunnable, mAutoScrollStartDelay + mAutoScrollDuration);
}
}
/**
* Restarts auto scrolling.
*/
public void restartAutoScroll() {
if (isAutoScroll()) {
removeCallbacks(mScrollRunnable);
postDelayed(mScrollRunnable, mAutoScrollStartDelay + mAutoScrollDuration);
}
}
/**
* Stops auto scrolling.
*/
public void stopAutoScroll() {
removeCallbacks(mScrollRunnable);
mScrollRunnable = null;
}
/**
* Sets auto scrolling speed.
*
* @param duration The duration in milliseconds when scrolling. Sets negative number to
* restore default.
*/
public void setAutoScrollSpeed(int duration) {
mAutoScrollSpeed = duration;
}
/**
* Sets auto scrolling speed and interpolator.
*
* @param duration The duration in milliseconds when scrolling. Sets negative number to
* restore default.
* @param interpolator The animation interpolator.
*/
public void setAutoScrollSpeed(int duration, Interpolator interpolator) {
mAutoScrollSpeed = duration;
mInterpolator = interpolator;
}
private void setScrollSpeed(int duration, Interpolator interpolator) {
try {
Field mViewFlinger = RecyclerView.class.getDeclaredField("mViewFlinger");
mViewFlinger.setAccessible(true);
Class viewFlingerClass = Class.forName("android.support.v7.widget" +
".RecyclerView$ViewFlinger");
Field mScroller = viewFlingerClass.getDeclaredField("mScroller");
mScroller.setAccessible(true);
Field mInterpolator = viewFlingerClass.getDeclaredField("mInterpolator");
mInterpolator.setAccessible(true);
Field mDecelerateInterpolator = LinearSmoothScroller.class.getDeclaredField(
"mDecelerateInterpolator");
mDecelerateInterpolator.setAccessible(true);
mInterpolator.set(mViewFlinger.get(this),
mDecelerateInterpolator.get(mSmoothScroller));
if (duration >= 0) {
UltraOverScroller overScroller = new UltraOverScroller(getContext(), interpolator);
overScroller.setScrollDuration(duration);
mScroller.set(mViewFlinger.get(this), overScroller);
} else {
OverScroller overScroller = new OverScroller(getContext(), interpolator);
mScroller.set(mViewFlinger.get(this), overScroller);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns whether this view is auto scrolled.
*
* @return True if this view is auto scrolled, or false otherwise.
*/
public boolean isAutoScroll() {
return mScrollRunnable != null;
}
/**
* Returns current position.
*
* @return Current position.
*/
public int getCurrentPosition() {
int position = -1;
if (getLayoutManager() instanceof LinearLayoutManager) {
LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
if (mAlignGravity == Gravity.END) {
position = layoutManager.findLastCompletelyVisibleItemPosition();
if (position < 0) {
if (mAlignMargin > 0) {
position = layoutManager.findLastVisibleItemPosition() - 1;
} else {
position = layoutManager.findLastVisibleItemPosition();
}
}
} else {
position = layoutManager.findFirstCompletelyVisibleItemPosition();
if (position < 0) {
if (mAlignMargin > 0) {
position = layoutManager.findFirstVisibleItemPosition() + 1;
} else {
position = layoutManager.findFirstVisibleItemPosition();
}
}
}
}
return position;
}
/**
* Returns real count.
*
* @return Real count.
*/
public int getRealCount() {
if (mRecyclerViewAdapter != null) {
return mRecyclerViewAdapter.getRealCount();
} else {
return 0;
}
}
/**
* Refresh adapter.
*/
public void refresh() {
if (mRecyclerViewAdapter != null) {
mRecyclerViewAdapter.notifyDataSetChanged();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mStopAutoScroll = true;
if (isAutoScroll()) {
removeCallbacks(mScrollRunnable);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mStopAutoScroll = false;
if (isAutoScroll()) {
removeCallbacks(mScrollRunnable);
postDelayed(mScrollRunnable, mAutoScrollDuration);
}
break;
default:
}
return super.dispatchTouchEvent(ev);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (isInfiniteLoop() && mRecyclerViewAdapter.getRealCount() > 0) {
int position =
Integer.MAX_VALUE / 2 - ((Integer.MAX_VALUE / 2) % mRecyclerViewAdapter.getRealCount());
scrollToPosition(position);
if (isPagerSnap() && getLayoutManager() != null) {
mSmoothScroller.setTargetPosition(position);
setScrollSpeed(0, null);
getLayoutManager().startSmoothScroll(mSmoothScroller);
}
}
restartAutoScroll();
}
@Override
protected void onDetachedFromWindow() {
if (isAutoScroll()) {
removeCallbacks(mScrollRunnable);
}
super.onDetachedFromWindow();
}
}