How to switch automatically between viewPager pages(如何在 viewPager 页面之间自动切换)
问题描述
我有一个 android 应用程序,它使用一个有两个页面的 ViewPager当活动首次显示时,我想将每个页面依次呈现给用户,以便他们知道他们可以在两个视图之间滑动.我找不到任何描述如何执行此操作的文档.我发现 PageTransformations 听起来很有希望,但用户必须先滑动.一旦 ViewPager 中的第一页显示,我需要我的两个页面自动滚动.怎样才能达到预期的效果?
I have an android application that employs a ViewPager with two pages When the activity first displays i would like to present each page in turn to the user so that they know they can swipe between to two views. I have failed to find any docs describing how to do this. I have discovered PageTransformations which sounded promising but the user has to swipe first. I need my two pages to scroll automatically as soon as the first page in the ViewPager displays. how can a achieve the desired result?
推荐答案
你可以使用 Timer 来达到这个目的.以下代码一目了然:
You can use Timer for this purpose. The following code is self explanatory:
// ---------------------------------------------------------------------------
Timer timer;
int page = 1;
public void pageSwitcher(int seconds) {
timer = new Timer(); // At this line a new Thread will be created
timer.scheduleAtFixedRate(new RemindTask(), 0, seconds * 1000); // delay
// in
// milliseconds
}
// this is an inner class...
class RemindTask extends TimerTask {
@Override
public void run() {
// As the TimerTask run on a seprate thread from UI thread we have
// to call runOnUiThread to do work on UI thread.
runOnUiThread(new Runnable() {
public void run() {
if (page > 4) { // In my case the number of pages are 5
timer.cancel();
// Showing a toast for just testing purpose
Toast.makeText(getApplicationContext(), "Timer stoped",
Toast.LENGTH_LONG).show();
} else {
mViewPager.setCurrentItem(page++);
}
}
});
}
}
// ---------------------------------------------------------------------------
注意1:确保在viewPager内部正确设置adapter后调用pageSwitcher方法onCreate 活动的方法.
Note 1: Make sure that you call pageSwitcher method after setting up adapter to the viewPager properly inside onCreate method of your activity.
注意 2:viewPager 会在您每次启动时滑动.您必须对其进行处理,使其仅在所有页面中滑动一次(当用户第一次查看 viewPager 时)
Note 2: The viewPager will swipe every time you launch it. You have to handle it so that it swipes through all pages only once (when the user is viewing the viewPager first time)
注意3:如果你想进一步减慢viewPager的滚动速度,可以在 StackOverflow 上关注这个答案.
Note 3: If you further want to slow the scrolling speed of the viewPager, you can follow this answer on StackOverflow.
如果这不能帮助你,请在评论中告诉我...
Tell me in the comments if that could not help you...
这篇关于如何在 viewPager 页面之间自动切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 viewPager 页面之间自动切换
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
