问题描述
我有一个Android应用程序,它采用了ViewPager有两页当活动首先显示我想present每一页依次给用户,让他们知道他们可以刷卡之间的两个观点。我并没有发现,描述如何做到这一点的任何文档。我发现PageTransformations这听起来很有前途,但用户必须先刷卡。我需要我的两个页面就自动滚动作为ViewPager显示第一页。如何才能达到预期的效果?
I have an android application that employs a ViewPager with two pagesWhen 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?
推荐答案
您可以使用定时器
用于这一目的。下面code是自我解释:
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:设置后,请确保您调用 pageSwitcher
方法适配器
到 viewPager
的活动将内部的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
,你可以的
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页面之间自动切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!