ViewPager 作为循环队列/包装 [英] ViewPager as a circular queue / wrapping
问题描述
我正在使用带有 FragmentStatePagerAdapter
的 ViewPager
以允许在某些片段之间导航.
I am using a ViewPager
with the FragmentStatePagerAdapter
to allow navigation between some fragments.
假设我有三个片段:A
、B
和 C
.ViewPager 最初显示 Fragment A,并允许您通过从右向左滑动导航到 Fragment B,然后再次滑动导航到 Fragment C.这允许以下导航路径: A <-->B<-->C
.
Let's say I have three fragments: A
, B
and C
. The ViewPager shows Fragment A initially, and allows you to navigate to Fragment B by swiping from right-to-left, and then to Fragment C by swiping again. This allows the following navigation paths: A <--> B <--> C
.
我想要的是能够在片段 A 上从左到右滑动并让 ViewPager 显示片段 C,即让它表现为循环队列并允许 ... -->C<--><-->B<-->C<-->一个 <-- ...
What I would like is to be able to swipe from left-to-right on Fragment A and have the ViewPager show Fragment C, i.e. for it to behave as a circular queue and allow ... --> C <--> A <--> B <--> C <--> A <-- ...
我不希望 Fragments 在其他位置重复(即以三个以上的实例结束).
I do not want the Fragments duplicated in other positions (i.e. ending up with more than three instances).
ViewPager 是否可以实现这种包装功能?
Is this wrapping functionality possible with a ViewPager?
推荐答案
我已经实现了一个 ViewPager/PagerAdapter,它可以允许伪无限分页行为.它的工作原理是指定一个非常大的数字作为实际计数,但将它们映射到数据集/页面集的实际范围.它还会将开头偏移一个大数字,以便您可以立即从第一"页向左滚动.
I've implemented a ViewPager/PagerAdapter that can allow for pseudo-infinite paging behaviour. It works by specifying a very large number as the actual count, but maps them to the actual range of the dataset/pageset. It offsets the beginning by a large number also so that you can immediately scroll to the left from the 'first' page.
一旦您到达第 1,000,000 页(滚动时您会看到图形故障),它就不会那么好用,但这通常不是现实世界的用例.我可以通过偶尔将计数重置为较低的数字来解决此问题,但我暂时保持原样.
It doesn't work so well once you get to 1,000,000th page (you will see graphical glitches when scrolling), but this is typically not a real-world use-case. I could fix this by resetting the count to a lower number once in a while, but I will leave it how it is for now.
InfinitePagerAdapter
包装了一个现有的 ViewPager,所以使用是非常透明的.InfiniteViewPager
做了一些工作,以确保您可以多次向左和向右滚动.
The InfinitePagerAdapter
wraps an existing ViewPager, and so the usage is quite transparent. The InfiniteViewPager
does does a bit of work to make sure you can potentially scroll to the left and right many times.
https://github.com/antonyt/InfiniteViewPager
这篇关于ViewPager 作为循环队列/包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!