更改ViewPager使无限页面滚动 [英] Changing ViewPager to enable infinite page scrolling

查看:281
本文介绍了更改ViewPager使无限页面滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

乔恩·威利斯发表了一篇关于如何使无限滚动和他的code。 在那里,他说,他做了ViewPager类的一些变化诠释了Android支持库。这已经作了修改,以及如何是否有可能重新编译图书馆与ViewPager变化?

Jon Willis has posted on how to enable an infinite scrolling with his code. In there he said that he made some changes in the ViewPager class int the android support library. Which changes have been made and how is it possible to "recompile" the library with the ViewPager change?

感谢

推荐答案

感谢您的回答Shereef。

Thank you for your answer Shereef.

我解决了它一点点不同。

I solved it a little bit differently.

我改变了ViewPager类支持Android库的code。该方法 setCurrentItem(INT)

I changed the code of the ViewPager class of the android support library. The method setCurrentItem(int)

改变动画的页面。此方法调用需要的索引和标志启用平滑滚动的内部方法。这个标志是布尔smoothScroll 。 扩展这种方法,第二个参数布尔smoothScroll 解决了这个问题对我来说。 调用此方法 setCurrentItem(INT指数,布尔smoothScroll)让我让它滚动下去。

changes the page with animation. This method calls an internal method that requires the index and a flag enabling smooth scrolling. This flag is boolean smoothScroll. Extending this method with a second parameter boolean smoothScroll solved it for me. Calling this method setCurrentItem(int index, boolean smoothScroll) allowed me to make it scroll indefinitely.

下面是一个完整的例子:

Here is a full example:

请考虑,只有中心页面显示。 而且做了我店的网页分开,让我来处理它们更轻松。

Please consider that only the center page is shown. Moreover did I store the pages seperately, allowing me to handle them with more ease.

private class Page {
  View page;
  List<..> data;
}
// page for predecessor, current, and successor
Page[] 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);
            }
        }
    });

然而,如果没有乔恩·威利斯code,我也不会解决它自己。

However, without Jon Willis Code I wouldn't have solved it myself.

编辑:这是一个关于这个博文:<一href="http://thehayro.blogspot.de/2013/09/infiniteviewpager-infinite-paging.html">http://thehayro.blogspot.de/2013/09/infiniteviewpager-infinite-paging.html

here is a blogpost about this: http://thehayro.blogspot.de/2013/09/infiniteviewpager-infinite-paging.html

这篇关于更改ViewPager使无限页面滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆