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);
}
}
});
}
}
这篇关于如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在视图寻呼机中自动更改图像,当它到达最后一张图像时,它应该自动到达第一张图像
基础教程推荐
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
