问题描述
我现在正在实现视图寻呼机,并且我想在特定时间段内自动连续不断地一次自动更改图像,可能是5毫秒一次.
I am now implementing the view pager and i want to continuously change the image one by one automatically for a particular periods of time may be once in 5 ms .
我还必须手动允许用户滑动图像(它工作正常)
I also have to manually allow the user to swipe the images (it is working properly)
但是图像不会自动更改,当到达最后一张图像时,它应该自动到达第一张图像
But the image is not automatically changing and when it reaches the last image it should come to the first image automatically
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == strImages.length-1) {
currentPage = 0;
}
intro_images.setCurrentItem(currentPage++, true);
}
};
timer = new Timer(); // This will create a new Thread
timer .schedule(new TimerTask() { // task to be scheduled
@Override
public void run() {
handler.post(Update);
}
}, 500, 3000);
我使用了上面的代码,但无法正常工作
I used above code but it does not work properly
推荐答案
尝试将addOnPageChangeListener
这样添加到您的viewPager
try this add addOnPageChangeListener
to your viewPager
like this
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == strImages.length-1) {
currentPage = 0;
}
intro_images.setCurrentItem(currentPage++, true);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
,或者对于自动更改页面,请尝试创建类似这样的时间任务
Timer timer;
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0, 3000); // delay*/
private class RemindTask extends TimerTask {
int current = viewPager.getCurrentItem();
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (current < strImages.size()) {
viewPager.setCurrentItem(current);
current += 1;
} else {
current = 0;
viewPager.setCurrentItem(current);
}
}
});
}
}
这篇关于当到达最后一张图像时,如何在视图寻呼机中自动更改图像,它应该自动出现在第一张图像上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!