-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoScrollRecyclerView
65 lines (57 loc) · 2.23 KB
/
AutoScrollRecyclerView
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
public class AutoScrollRecyclerView extends RecyclerView {
private static long delayTime = 45;// How long after the interval to perform scrolling
AutoPollTask autoPollTask;// Scroll thread
private boolean running; // Is it rolling?
private boolean canRun;// Can it be automatically scrolled, depending on whether the data exceeds the screen
public AutoScrollRecyclerView(Context context) {
super(context);
}
public AutoScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
autoPollTask = new AutoPollTask(this);// Instantiate the scroll refresh thread
}
static class AutoPollTask implements Runnable {
private final WeakReference<AutoScrollRecyclerView> mReference;
// Use weak references to hold external class references to prevent memory leaks
public AutoPollTask(AutoScrollRecyclerView reference) {
this.mReference = new WeakReference<>(reference);
}
@Override
public void run() {
AutoScrollRecyclerView recyclerView = mReference.get();// Get the recyclerview object
if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
recyclerView.scrollBy(2, 2);// Note the difference between scrollBy and scrollTo
//delayed to send
recyclerView.postDelayed(recyclerView.autoPollTask, delayTime);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
/*if (running)
stop();*/
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
/*if (canRun)
start();*/
break;
}
return super.onTouchEvent(e);
}
//Open: If it is running, stop first -> then open
public void start() {
if (running)
stop();
canRun = true;
running = true;
postDelayed(autoPollTask, delayTime);
}
public void stop() {
running = false;
removeCallbacks(autoPollTask);
}
}