猿问

更改ViewPager以启用无限页面滚动

Jon Willis发布了如何使用他的代码实现无限滚动。在那里,他说他在Android支持库的ViewPager类中进行了一些更改。已经进行了哪些更改以及如何使用ViewPager更改“重新编译”库?


慕哥9229398
浏览 603回答 3
3回答

月关宝盒

这是我的代码: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 {&nbsp; View page;&nbsp; List<..> data;}// page for predecessor, current, and successorPage[] pages = new Page[3];mDayPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onPageSelected(int position) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onPageScrollStateChanged(int state) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (state == ViewPager.SCROLL_STATE_IDLE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mFocusedPage == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move some stuff from the&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // center to the right here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveStuff(pages[1], pages[2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move stuff from the left to the center&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveStuff(pages[0], pages[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // retrieve new stuff and insert it to the left page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertStuff(pages[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (mFocusedPage == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move stuff from the center to the left page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveStuff(pages[1], pages[0]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move stuff from the right to the center page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveStuff(pages[2], pages[1]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // retrieve stuff and insert it to the right page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertStuff(pages[2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // go back to the center allowing to scroll indefinitely&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mDayPager.setCurrentItem(1, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });但是,如果没有Jon Willis Code,我自己也不会解决它。

白衣染霜花

通过覆盖现有适配器类中的4个适配器方法,无限视图分页器&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getCount() {&nbsp; &nbsp; &nbsp; &nbsp; return Integer.MAX_VALUE;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public CharSequence getPageTitle(int position) {&nbsp; &nbsp; &nbsp; &nbsp; String title = mTitleList.get(position % mActualTitleListSize);&nbsp; &nbsp; &nbsp; &nbsp; return title;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object instantiateItem(ViewGroup container, int position) {&nbsp; &nbsp; &nbsp; &nbsp; int virtualPosition = position % mActualTitleListSize;&nbsp; &nbsp; &nbsp; &nbsp; return super.instantiateItem(container, virtualPosition);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void destroyItem(ViewGroup container, int position, Object object) {&nbsp; &nbsp; &nbsp; &nbsp; int virtualPosition = position % mActualTitleListSize;&nbsp; &nbsp; &nbsp; &nbsp; super.destroyItem(container, virtualPosition, object);&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Android
我要回答