-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoLoopViewPager.java
135 lines (114 loc) · 3.71 KB
/
AutoLoopViewPager.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
package com.ststudy.testsomthing;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.PagerAdapter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import java.lang.ref.WeakReference;
/**
* 继承LoopViewPager,增加handle触发自动循环,并对事件进行处理,按下停止播放,抬起开始播放
*/
public class AutoLoopViewPager extends LoopViewPager {
public static final int DEFAULT_INTERVAL = 4000;
public static final int SCROLL_WHAT = 0;
private long interval = DEFAULT_INTERVAL;
private Handler handler;
public AutoLoopViewPager(Context context) {
this(context, null);
}
public AutoLoopViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
handler = new MyHandler(this);
startAutoScroll();
}
private static class MyHandler extends Handler {
private final WeakReference<AutoLoopViewPager> autoScrollViewPager;
public MyHandler(AutoLoopViewPager autoScrollViewPager) {
this.autoScrollViewPager = new WeakReference<AutoLoopViewPager>(autoScrollViewPager);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SCROLL_WHAT:
AutoLoopViewPager pager = this.autoScrollViewPager.get();
if (pager != null) {
pager.scrollOnce();
pager.sendScrollMessage(pager.interval);
}
default:
break;
}
}
}
/**
* <ul>
* if stopScrollWhenTouch is true
* <li>if event is down, stop auto scroll.</li>
* <li>if event is up, start auto scroll again.</li>
* </ul>
*/
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_DOWN) {
stopAutoScroll();
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
startAutoScroll();
}
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
/**
* scroll only once
*/
public void scrollOnce() {
PagerAdapter adapter = getAdapter();
int currentItem = getCurrentItem();
int totalCount;
if (adapter == null || (totalCount = adapter.getCount()) <= 1) {
return;
}
int nextItem = ++currentItem;
if (nextItem < 0) {
setCurrentItem(totalCount - 1);
} else if (nextItem == totalCount) {
setCurrentItem(0);
} else {
setCurrentItem(nextItem);
}
}
/**
* start auto scroll, first scroll delay time is 4s
*/
public void startAutoScroll() {
sendScrollMessage(interval);
}
/**
* start auto scroll
*
* @param delayTimeInMills first scroll delay time
*/
public void startAutoScroll(int delayTimeInMills) {
sendScrollMessage(delayTimeInMills);
}
/**
* stop auto scroll
*/
public void stopAutoScroll() {
handler.removeMessages(SCROLL_WHAT);
}
private void sendScrollMessage(long delayTimeInMills) {
/** remove messages before, keeps one message is running at most **/
handler.removeMessages(SCROLL_WHAT);
handler.sendEmptyMessageDelayed(SCROLL_WHAT, delayTimeInMills);
}
public void setInterval(long interval) {
this.interval = interval;
}
}