可以将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中制作这些碎片autoslide。
答案 0 :(得分:12)
在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();
}
答案 1 :(得分:11)
如果您想要拖动模拟,可能应该查看以下API ViewPager.beginFakeDrag(),ViewPager. fakeDragBy(float offset)和ViewPager.endFakeDrag()。此外,setCurrentItem() API提供了设置当前页面并顺利完成的功能。 我建议自动制作幻灯片的最简单方法是使用简单的Runnable设置Handler,这将调用寻呼方法来设置活动中的项目,并为它执行postDelayed()。并且不要忘记在用户交互时调用removeCallbacks()或者例如活动暂停。
答案 2 :(得分:1)
简短答案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();
}
答案 3 :(得分:0)
首先创建带有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);
}
});
}
}
下一步创建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) {
}