简体   繁体   English

使用动态加载程序在没有ListFragments的ViewPager中加载ListView页面的模式?

[英]Pattern for using dynamic loaders to load ListView pages in a ViewPager without ListFragments?

If I have a ViewPager with a certain number of pages, what would be the best way of loading the data for each page using the Loader framework? 如果我的ViewPager具有一定数量的页面,那么使用Loader框架为每个页面加载数据的最佳方法是什么? Each page contains a ListView with its own adapter instance. 每个页面都包含一个ListView及其自己的适配器实例。

  1. Use a separate loader for each page; 每页使用一个单独的加载器; or 要么
  2. Use a single loader that checks which pages are not loaded and loads them 使用单个加载程序检查未加载哪些页面并加载它们

The thing is, it's possible the user may want to quickly swipe through a bunch of pages and thus it should be easy to "cancel" loading pages that aren't needed for performance reasons. 问题是,用户可能希望快速滑动一堆页面,因此应该很容易“取消”加载出于性能原因而不需要的页面。

On the other hand it seems like there might be an overhead with having a lot of loaders. 另一方面,拥有许多装载机似乎可能会产生开销。

EDIT: My Loader s actually need to have dynamic IDs to represent the content they are loading. 编辑:我的Loader实际上需要具有动态ID来表示他们正在加载的内容。 I'm probably going to go with using a mask on the loader ID, and then once the loader is done call destroyLoader() . 我可能会在加载程序ID上使用掩码,然后在完成加载程序后调用destroyLoader()

I would use a separate Loader for each ListFragment . 我将为每个ListFragment使用单独的Loader Things will get messy very quickly if you try to manage the fragments' Loader s from the parent Activity (especially if those fragments are being held by a ViewPager ). 如果您尝试通过父Activity管理片段的Loader事情将会变得非常混乱(特别是如果这些片段是由ViewPager持有的ViewPager )。 Fragment s are supposed to be designed for reuse and should not rely on a specific Activity in order to function properly. Fragment s被设计为可重用,并且不应依赖于特定的Activity才能正常运行。 The fact that each Activity and each Fragment get their own LoaderManager instance is further evidence that the Android team didn't want developers to have their Activity s loading data for its attached Fragment s. 每个Activity和每个Fragment都有自己的LoaderManager实例,这进一步证明了Android团队不希望开发人员让Activity为其附加的Fragment加载数据。

I don't think there will be as much overhead as you are probably expecting there to be... Loader s should retain their data across the component's lifecycle and re-query only when the underlying content has changed. 我认为开销不会像您预期的那样大。... Loader应当在组件的整个生命周期中保留其数据,并且仅在基础内容已更改时才重新查询。 It might be a good idea to use a FragmentStatePagerAdapter too. 最好也使用FragmentStatePagerAdapter

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

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