如何在 viewPager 页面之间自动切换 [英] How to switch automatically between viewPager pages

查看:15
本文介绍了如何在 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 页面之间自动切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆