0

我有一个 ViewPager,我正在填充里面有一个列表视图的片段。关键是我必须展示大量的数据,而且我需要制作页面,所以我只请求少量的数据。

我的问题是我需要在页面更改时调用异步任务来检索数据,并填写此页面的列表视图,我该怎么做?如何在任务的 onPostExecute 中更改该列表视图?

PS:我已经为标签+滑动活动使用了一个 Eclipse 模板,所以我没有发布我的代码。

4

1 回答 1

1

I'm not sure I got your problem right, but the first thing that comes to my mind, is that I would use an AsyncTaskLoader instead of a simple AsyncTask. From my (limited) experience, loaders solve a lot of problems when it comes to Fragment/Activity lifecycle/configuration changes.

Loaders guide (Android Developers)

No matter what method you are using to get the data, changing the content of the list view in page B after loading the data in page A shouldn't be much of a problem: you have plenty of options, from simply saving the data for page B in the Activity (then changing the page with setCurrentItem and intercepting the page change event with setOnPageChangeListener), to a more elegant approach employing a SQLite database (which btw would allow you to do some caching on the results, if possible).

Oh, and let's not forget that, if you are using an implementation of the abstract class FragmentPagerAdapter you can probably pass the data directly from one page to the other, as this PagerAdapter implementation

represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.

(just use getItem(int) to get a reference to the page you need. I never tried this myself, though).

Hope this helps

EDIT I just found out that getting a reference to the current Fragment shown in the ViewPager is tricky if you are not using a FragmentPagerAdapter: if this was the root of your problem, information about how it can be done can be found here:

Update data in ListFragment as part of ViewPager

Retrieve a Fragment from a ViewPager

Communication between ViewPager and current Fragment

于 2013-05-27T20:41:52.080 回答