how to change the image automatically in view pager when it reaches the last image it should come to the first image automatically(如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像)
问题描述
我现在正在实现视图寻呼机,我想在特定的时间段内不断地自动更改图像,可能是每 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);
}
}
});
}
}
这篇关于如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像


基础教程推荐
- 如何使 UINavigationBar 背景透明? 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01