如何在 viewPager 页面之间自动切换

How to switch automatically between viewPager pages(如何在 viewPager 页面之间自动切换)
本文介绍了如何在 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 页面之间自动切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)