如何自动滑动android View Pager

How to auto slide android View Pager(如何自动滑动android View Pager)
本文介绍了如何自动滑动android View Pager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将 View Pager 设置为自动滑动或自动翻页.我将我的 viewpager 设置为使用如下所示的适配器,它工作正常:-

Can a View Pager be made to auto slide or autopage. I have my viewpager set up to use the adapter like the below and it works fine:-

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.gridslide);
ImagePagerAdapter mAdapter = new ImagePagerAdapter(
        getSupportFragmentManager(),4);
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}

适配器如下:-

public static class ImagePagerAdapter extends FragmentStatePagerAdapter {
private final int mSize;

public ImagePagerAdapter(FragmentManager fm, int size) {
    super(fm);
    mSize = size;
}

@Override
public int getCount() {
    return mSize;
}
@Override
public Fragment getItem(int position) {
    Log.v(TAG,"position="+position);
    return TheFragment.newInstance(position);
}}

但是我想知道如何让这些片段在 viewpager 中自动滑动.

However I would want to know how to make these fragments autoslide in a viewpager.

推荐答案

在 setCurrentItem(int item, boolean smoothScroll) 中设置 smoothScroll = true 并不总是有平滑滚动的效果.假设如果您的 viewpager 中的页面少于 5 个,您几乎不会注意到平滑滚动.

Setting smoothScroll = true in setCurrentItem(int item, boolean smoothScroll) does not always have the smooth scroll effect. Suppose if you have less than 5 pages in you viewpager, you will hardly notice the smooth scroll.

在这种情况下,最困难的方法是把它放在一个 for 循环中

In this scenario, the hard way to do it is to put it in a for loop

//This will scroll page-by-page so that you can view scroll happening
for (int i = 0; i < mAdapter.getCount()-1; i++)
    mPager.setCurrentItem(i, true);

如果有人需要更慢的滚动,他们可以像这样使用 postDelayed()...

If some one needs more slower scroll, they can use postDelayed() like this...

static int i=0;
private final Handler handler = new Handler();
somefunction()
{
    handle.post(ViewPagerVisibleScroll);
}

    Runnable ViewPagerVisibleScroll= new Runnable() {
                @Override
                public void run() {
                    if(i <= mAdapter.getCount()-1)
                    {
                        mPager.setCurrentItem(i, true);
                        handle.postDelayed(TopChartAnimation, 100);
                        i++;
                    }
                }
            };

总是不推荐休眠:如果有人需要更慢的滚动,他们可能会在这个 for 循环中使用休眠...

Sleep is always NOT recommended : If some one needs more slower scroll, they may use a sleep in this for loop...

@Override
public void onClick(View v) {
Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < mAdapter.getCount()-1; i++) {
                        final int value = i;
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mPager.setCurrentItem(value, true);
                            }
                        });
                    }
                }
            };
            new Thread(runnable).start();

        }

这篇关于如何自动滑动android View Pager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

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)