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

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

问题描述

Jon Willis 已发布关于如何使用他的代码启用无限滚动的帖子.

Jon Willis has posted on how to enable an infinite scrolling with his code.

在那里,他说他在 android 支持库中的 ViewPager 类中做了一些更改.进行了哪些更改以及如何重新编译"?ViewPager 更改的库?

In there he said that he made some changes in the ViewPager class in 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.

我改了android支持库的ViewPager类的代码.方法 setCurrentItem(int)

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

用动画改变页面.此方法调用需要索引和启用平滑滚动的标志的内部方法.这个标志是boolean smoothScroll.使用第二个参数 boolean smoothScroll 扩展此方法为我解决了这个问题.调用这个方法 setCurrentItem(int index, boolean 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.

这是一个完整的例子:

请注意只显示了中心页面.此外,我将页面分开存储,让我更轻松地处理它们.

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);
            }
        }
    });

但是,如果没有 Jon Willis 代码,我将无法自己解决.

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

这是关于此的博客文章:

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

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