-
月关宝盒
这是我的代码:public class MyPagerAdapter extends FragmentStatePagerAdapter{ public static int LOOPS_COUNT = 1000; private ArrayList<Product> mProducts; public MyPagerAdapter(FragmentManager manager, ArrayList<Product> products) { super(manager); mProducts = products; } @Override public Fragment getItem(int position) { if (mProducts != null && mProducts.size() > 0) { position = position % mProducts.size(); // use modulo for infinite cycling return MyFragment.newInstance(mProducts.get(position)); } else { return MyFragment.newInstance(null); } } @Override public int getCount() { if (mProducts != null && mProducts.size() > 0) { return mProducts.size()*LOOPS_COUNT; // simulate infinite by big number of products } else { return 1; } }} 然后,在ViewPager中,我们将当前页面设置为中间:mAdapter = new MyPagerAdapter(getSupportFragmentManager(), mProducts);mViewPager.setAdapter(mAdapter);mViewPager.setCurrentItem(mViewPager.getChildCount() * MyPagerAdapter.LOOPS_COUNT / 2, false); // set current item in the adapter to middle
-
慕尼黑的夜晚无繁华
感谢谢谢你的回答。我有点不同地解决了它。我更改了android支持库的ViewPager类的代码。方法setCurrentItem(int)用动画更改页面。此方法调用需要索引的内部方法和允许平滑滚动的标志。这个标志是boolean smoothScroll。使用第二个参数扩展此方法boolean smoothScroll为我解决了这个问题。调用此方法setCurrentItem(int index, boolean smoothScroll)允许我无限期地滚动它。这是一个完整的例子:请注意只显示中心页面。此外,我是否单独存储页面,让我更轻松地处理它们。private class Page { View page; List<..> data;}// page for predecessor, current, and successorPage[] pages = new Page[3];mDayPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_IDLE) { if (mFocusedPage == 0) { // move some stuff from the // center to the right here moveStuff(pages[1], pages[2]); // move stuff from the left to the center moveStuff(pages[0], pages[1]); // retrieve new stuff and insert it to the left page insertStuff(pages[0]); } else if (mFocusedPage == 2) { // move stuff from the center to the left page moveStuff(pages[1], pages[0]); // move stuff from the right to the center page moveStuff(pages[2], pages[1]); // retrieve stuff and insert it to the right page insertStuff(pages[2]); } // go back to the center allowing to scroll indefinitely mDayPager.setCurrentItem(1, false); } } });但是,如果没有Jon Willis Code,我自己也不会解决它。
-
白衣染霜花
通过覆盖现有适配器类中的4个适配器方法,无限视图分页器 @Override public int getCount() { return Integer.MAX_VALUE; } @Override public CharSequence getPageTitle(int position) { String title = mTitleList.get(position % mActualTitleListSize); return title; } @Override public Object instantiateItem(ViewGroup container, int position) { int virtualPosition = position % mActualTitleListSize; return super.instantiateItem(container, virtualPosition); } @Override public void destroyItem(ViewGroup container, int position, Object object) { int virtualPosition = position % mActualTitleListSize; super.destroyItem(container, virtualPosition, object); }