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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I solved this problem very simply using a little hack in the adapter. Here is my code:
And then, in the ViewPager, we set current page to the middle:
Its hacked by CustomPagerAdapter:
MainActivity.java:
activity_main.xml:
row_item_viewpager.xml:
Done
infinite slider adapter skeleton based on previous samples
some critical issues:
you may look at the logcat to understand whats happening in this sample
And then:
Actually, I've been looking at the various ways to do this "infinite" pagination, and even though the human notion of time is that it is infinite (even though we have a notion of the beginning and end of time), computers deal in the discrete. There is a minimum and maximum time (that can be adjusted as time goes on, remember the basis of the Y2K scare?).
Anyways, the point of this discussion is that it is/should be sufficient to support a relatively infinite date range through an actually finite date range. A great example of this is the Android framework's
CalendarView
implementation, and theWeeksAdapter
within it. The default minimum date is in 1900 and the default maximum date is in 2100, this should cover 99% of the calendar use of anyone within a 10 year radius around today easily.What they do in their implementation (focused on weeks) is compute the number of weeks between the minimum and maximum date. This becomes the number of pages in the pager. Remember that the pager doesn't need to maintain all of these pages simultaneously (
setOffscreenPageLimit(int)
), it just needs to be able to create the page based on the page number (or index/position). In this case the index is the number of weeks that the week is from the minimum date. With this approach you just have to maintain the minimum date and the number of pages (distance to the maximum date), then for any page you can easily compute the week associated with that page. No dancing around the fact thatViewPager
doesn't support looping (a.k.a infinite pagination), and trying to force it to behave like it can scroll infinitely.Then
WeekDaysFragment
can easily display the week starting at the date passed in its arguments.Alternatively, it seems that some version of the Calendar app on Android uses a
ViewSwitcher
(which means there's only 2 pages, the one you see and the hidden page). It then changes the transition animation based on which way the user swiped and renders the next/previous page accordingly. In this way you get infinite pagination because it just switching between two pages infinitely. This requires using aView
for the page though, which is way I went with the first approach.In general, if you want "infinite pagination", it's probably because your pages are based off of dates or times somehow. If this is the case consider using a finite subset of time that is relatively infinite instead. This is how
CalendarView
is implemented for example. Or you can use theViewSwitcher
approach. The advantage of these two approaches is that neither does anything particularly unusual with theViewSwitcher
orViewPager
, and doesn't require any tricks or reimplementation to coerce them to behave infinitely (ViewSwitcher
is already designed to switch between views infinitely, butViewPager
is designed to work on a finite, but not necessarily constant, set of pages).I built a library that can make any ViewPager, pagerAdapter (or FragmentStatePagerAdapter), and optional TabLayout infinitely Scrolling.
https://github.com/memorex386/infinite-scroll-viewpager-w-tabs
Thank you for your answer Shereef.
I solved it a little bit differently.
I changed the code of the ViewPager class of the android support library. The method
setCurrentItem(int)
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 parameterboolean smoothScroll
solved it for me. Calling this methodsetCurrentItem(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.
However, without Jon Willis Code I wouldn't have solved it myself.
EDIT: here is a blogpost about this: