9

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

@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 中自动滑动。

4

4 回答 4

13

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

在这种情况下,最困难的方法是将其放入 for 循环中

//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() ...

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 循环中使用睡眠......

@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();

        }
于 2013-11-13T10:55:03.540 回答
11

可能,您应该看看以下 API ViewPager.beginFakeDrag()ViewPager。fakeDragBy(float offset)ViewPager.endFakeDrag()如果你想要拖动模拟。此外,setCurrentItem() API 提供了设置当前页面并顺利完成的能力。我建议自动制作幻灯片的最简单方法是使用简单的 Runnable 设置Handler,它会调用寻呼机方法来设置活动中的项目,然后为它执行 postDelayed()。并且不要忘记在用户交互或活动暂停时为其调用 removeCallbacks()。

于 2012-07-28T10:49:43.227 回答
4

首先使用 TimerTask 创建 Slider 类扩展

public class SliderTimer extends TimerTask {
    private ViewPager viewPager;
    private int size;
    private Activity activity;

    public SliderTimer(ViewPager viewPager, int size, Activity activity) {
        this.viewPager = viewPager;
        this.size = size;
        this.activity = activity;
    }

    @Override
    public void run() {
        activity.runOnUiThread(() -> {
            if (viewPager.getCurrentItem() < size - 1) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
            } else {
                viewPager.setCurrentItem(0, true);
            }
        });
    }
}

Next 创建 SpeedSlowScroller 用于慢速滚动

public class SpeedSlowScroller extends Scroller {

    private int mDuration = 2500;

    public SpeedSlowScroller(Context context) {
        super(context);
    }

    public SpeedSlowScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    public SpeedSlowScroller(Context context, Interpolator interpolator, boolean flywheel) {
        super(context, interpolator, flywheel);
    }


    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }
}

最后将SpeedSlowScroller添加到 ViewPager 中,并添加SliderTimer用于自动滚动

try {
 Field mScroller = ViewPager.class.getDeclaredField("mScroller");
 mScroller.setAccessible(true);
 SpeedSlowScroller scroller = new SpeedSlowScroller(_context);
 mScroller.set(your_viewpager, scroller);
 Timer timer = new Timer();
 timer.scheduleAtFixedRate(new SliderTimer(your_viewpager, models.size(), activity), 4000, 6000);
 } catch (Exception ignored) {
 }
于 2019-05-02T12:11:28.430 回答
3

简短的回答 2020

设置 viewPager 适配器后

   private Runnable runnable = null;
  
   public void function DisplaySlider(){

    sliderItemAdapter = new SliderItemAdapter(getActivity(), mSliderList);//push the data to the adapter
    mViewPager.setAdapter(sliderItemAdapter); //set the adapter to the view pager
    startAutoSlider(sliderItem.getCount());

   }


     

private void startAutoSlider(final int count) {

    runnable = new Runnable() {
        @Override
        public void run() {
            int pos = mViewPager.getCurrentItem();
            pos = pos + 1;
            if (pos >= count) pos = 0;
            mViewPager.setCurrentItem(pos);
            handler.postDelayed(runnable, 3000);
        }
    };
    handler.postDelayed(runnable, 3000);
}

   

最后不要忘记从回调中释放内存

@Override
public void onDestroy() {
    if (runnable != null) handler.removeCallbacks(runnable);
    super.onDestroy();
}
于 2020-10-30T08:22:26.440 回答