android的ViewPager实现自动播放

上次实现了ViewPager的循环播放android的ViewPager实现伪循环效果, 这次来实现自动播放.

非常简单, 直接用ScheduledExecutorService就好了.

原文

android的ViewPager实现自动播放

Code

直接看代码, 看注释.

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
package com.qefee.pj.testviewpager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
/**
* log tag for MainActivity
*/

private static final String TAG = "MainActivity";

ViewPager viewPager;

String[] texts = {
"hello",
"world",
"1",
"2"
};

TextView[] textViews;

int currentItem;
private ScheduledExecutorService executor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

int size = initSize();
initTextViews(size);
initViewPager();
}

@Override
protected void onResume() {
super.onResume();

startAutoScroll(); // activity激活时候自动播放
}

@Override
protected void onPause() {
super.onPause();

stopAutoScroll(); // activity暂停时候停止自动播放
}

private void startAutoScroll() {
stopAutoScroll();

executor = Executors.newSingleThreadScheduledExecutor();
Runnable command = new Runnable() {
@Override
public void run() {
selectNextItem();
}

private void selectNextItem() {
runOnUiThread(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(++currentItem);
}
});
}
};
int delay = 2;
int period = 2;
TimeUnit timeUnit = TimeUnit.SECONDS;
executor.scheduleAtFixedRate(command, delay, period, timeUnit);
}

private void stopAutoScroll() {
if (executor != null) {
executor.shutdownNow();
}
}

private void initViewPager() {
viewPager = (ViewPager) findViewById(R.id.viewPage);

viewPager.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return Integer.MAX_VALUE; // 取一个大数字
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.i(TAG, "instantiateItem: instantiateItem + position = " + position);
TextView t = textViews[position % textViews.length];
container.addView(t);

return t;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.i(TAG, "destroyItem: + position = " + position);
container.removeView((View) object);
}
});

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
currentItem = position;

startAutoScroll(); // 手动切换完成后恢复自动播放
}

@Override
public void onPageScrollStateChanged(int state) {

}
});

currentItem = texts.length * 1000000; // 取一个中间的大数字, 防止接近边界
viewPager.setCurrentItem(currentItem);
}

private void initTextViews(int size) {
TextView[] tvs = new TextView[size];

for (int i = 0; i < tvs.length; i++) {
tvs[i] = new TextView(this);
tvs[i].setText(texts[i % texts.length]);

int color = 0;

switch (i % texts.length) {
case 0:
color = Color.parseColor("#aaaaaa");
break;
case 1:
color = Color.parseColor("#cccccc");
break;
case 2:
color = Color.parseColor("#777777");
break;
case 3:
color = Color.parseColor("#999999");
break;
default:
break;
}

tvs[i].setBackgroundColor(color);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
tvs[i].setLayoutParams(layoutParams);
textViews = tvs;
}
}

private int initSize() {
int size;
if (texts.length > 3) {
size = texts.length;
} else {
size = texts.length * 2; // 小于3个时候, 需要扩大一倍, 防止出错
}
return size;
}
}
Fork me on GitHub