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 页面之间自动切换
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01