可以将 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!