简体   繁体   English

ViewPager可能包含无限数量的页面

[英]ViewPager for potentially infinite number of pages

I am currently using a ViewPager object to create a gallery of dynamically loaded photos (from a remote server). 我目前正在使用ViewPager对象创建动态加载的照片库(从远程服务器)。 I noticed that a ViewPager object relies heavily on knowing the number of pages (or pictures in my case) in advance, while in my situation I don't know. 我注意到一个ViewPager对象在很大程度上依赖于事先知道页面数(在我的情况下为图片),而在我的情况下我不知道。

Am I right? 我对吗? And if I am, is there an alternative that will make sliding between pictures very easy (that's what I like about ViewPager)? 如果我愿意,是否有其他选择可以使图片之间的滑动变得非常容易(这就是我对ViewPager的喜好)?

Thanks in advance 提前致谢

Am I right? 我对吗?

Well, you need to have a getCount() method on your PagerAdapter that returns something. 好吧,您需要在PagerAdapter上有一个getCount()方法,该方法返回一些信息。 If you wish to define that as "relies heavily", then, use, it "relies heavily". 如果您希望将其定义为“高度依赖”,请使用“高度依赖”。

And if I am, is there an alternative that will make sliding between pictures very easy (that's what I like about ViewPager)? 如果我愿意,是否有其他选择可以使图片之间的滑动变得非常容易(这就是我对ViewPager的喜好)?

Use ViewPager . 使用ViewPager

While your images may be loaded dynamically, probably the count of images is knowable in advance. 虽然可以动态加载图像,但可能事先知道图像的数量。 In that case, retrieve that value, then use that for getCount() . 在这种情况下,请检索该值,然后将其用于getCount()

Otherwise, have getCount() return 1000000 or something similarly huge. 否则,让getCount()返回1000000或类似的值。 If you find you reach the end of your available pictures, call notifyDataSetChanged() and have your PagerAdapter return the right value from getCount() (since you know it now). 如果找到可用图片的末尾,请调用notifyDataSetChanged()并使PagerAdaptergetCount()返回正确的值(因为现在知道了)。 Or, have the remaining pages show some placeholder. 或者,让其余页面显示一些占位符。

For Infinite Scrolling , I figured out one solution to it. 对于“ 无限滚动” ,我想出了一种解决方案。

Here's how I am doing it: 这是我的做法:

No looping is required in this case. 在这种情况下,不需要循环。 No matter what your getCount() is. 无论您的getCount()是什么。

After setting the pager adapter, I am setting a setOnPageChangeListener to the view pager like this: 设置寻呼机适配器后,我将setOnPageChangeListener设置为视图寻呼机,如下所示:

myPager.setOnPageChangeListener(new OnPageChangeListener() {

                @Override
                public void onPageSelected(int position) {
//Define the focused page before your onCreate()..private int focusedPage = 0;
                    focusedPage = position;
                }
                //We don't have to do anything here.
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub

                }
                //Here's where the magic is. After you reach the end of the page, you can scroll again and it will move your view to position you desire to put. In my case it's 0.
                @Override
                public void onPageScrollStateChanged(int arg0) {

                    myPager.setCurrentItem(0,false);

                }
            });

I know it's too late to answer this question but this was the solution I found out. 我知道回答这个问题为时已晚,但这是我找到的解决方案。 Maybe helpful for other users. 可能对其他用户有帮助。

It's working perfect for me...Any suggestions to make this answer better will really help me too. 它对我来说非常完美...任何使这个答案更好的建议也将对我有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM