ViewPager 的无尽适配器 [英] Endless adapter for ViewPager

查看:14
本文介绍了ViewPager 的无尽适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 CWAC 的 EndlessAdapter 来实现 ListView 的无限滚动.

I've been using CWAC's EndlessAdapter to achieve infinite scrolling on ListViews.

我想完成 ViewPager 的等效操作.不幸的是,PageAdapter 和 ListAdapter 不共享同一个公共基类.

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?

将无尽"逻辑添加到您自己的 PagerAdapter 实现中.或者,如果您愿意,可以尝试创建一个装饰 PagerAdapter,就像 EndlessAdapter 装饰常规 Adapter 一样.

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.

后者可能比较棘手,因为 PagerAdapter 是为将页面设计为视图或片段的,并且像 FragmentPagerAdapter 这样的类内部的片段处理有点吓人.

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?

我不知道.

主要是因为用例似乎没有那么引人注目.使用 ListView,用户可以快速翻阅列表,快速滚动数十或数百行.因此,使用we got to end"作为加载更多数据的触发器似乎是合理的.但是,使用 ViewPager 时,通常需要更长的时间才能到达终点,尤其是在您不使用 PagerTabStrip 或同等功能的情况下.因此,等到用户一直到最后才开始加载额外的数据似乎会让用户感到厌烦——你一直有时间去检索更多的数据,但没有使用它.

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.

因此,您可以使用 ViewPager 注册一个 ViewPager.OnPageChangeListener.当 onPageSelected() 并且您认为自己接近尾声时,启动一个 AsyncTask(或其他)以收集更多数据.然后问题是,您需要更新 PagerAdapter 使用的数据,并在数据更新后在该适配器上调用 notifyDataSetChanged().

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.

这篇关于ViewPager 的无尽适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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