问题
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? Each page contains a ListView with its own adapter instance.
- Use a separate loader for each page; or
- 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. I'm probably going to go with using a mask on the loader ID, and then once the loader is done call destroyLoader()
.
回答1:
I would use a separate Loader
for each ListFragment
. 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
). Fragment
s are supposed to be designed for reuse and should not rely on a specific Activity
in order to function properly. 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.
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. It might be a good idea to use a FragmentStatePagerAdapter
too.
来源:https://stackoverflow.com/questions/12023568/pattern-for-using-dynamic-loaders-to-load-listview-pages-in-a-viewpager-without