I've been using CWAC's EndlessAdapter to achieve infinite scrolling on ListViews.
I'd like to accomplish the equivalent for a ViewPager. Unfortunately, PageAdapter and ListAdapter do not share the same common base class.
What's the best way to go about this? Does a library exist that already handles this?
What's the best way to go about this?
Add "endless" logic to your own implementation of PagerAdapter
. Or, if you wish, try creating a decorating PagerAdapter
, the way that EndlessAdapter
decorates a regular Adapter
.
The latter is likely to be tricky, given that PagerAdapter
is designed for pages to be views or fragments, and the fragment handling inside of classes like FragmentPagerAdapter
is a bit scary.
Does a library exist that already handles this?
None that I am aware of.
Mainly, that is because the use case doesn't seem as compelling. With a ListView
, the user can fling the list, scrolling through dozens or hundreds of rows very quickly. Hence, using "we got to the end" as the trigger to load more data seems reasonable. With a ViewPager
, though, it typically takes a lot longer to get to the end, particularly if you are not using PagerTabStrip
or the equivalent. Hence, waiting until the user gets all the way to the end to begin loading additional data seems like it would be annoying to the user -- you had all this time to go retrieve more data, but didn't use it.
An alternative, therefore, is for you to register a ViewPager.OnPageChangeListener
with your ViewPager
. When onPageSelected()
, and you consider yourself to be close to the end, kick off an AsyncTask
(or whatever) to go gather more data. The catch then is that you will need to update the data used by the PagerAdapter
and call notifyDataSetChanged()
on that adapter once the data has been updated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With