AsynLoaderTask和ViewPager [英] AsynLoaderTask and ViewPager

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

问题描述

我有一个带有FragmentStatePagerAdapter的ViewPager.在ViewPager片段的onViewCreated方法中,我调用LoadManager的initLoader方法来启动这样的AsyncTaskLoader

I have a ViewPager with a FragmentStatePagerAdapter. In the onViewCreated method of the ViewPager fragments I call the initLoader method of the LoadManager to start an AsyncTaskLoader like this

    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = (ListView) view.findViewById(R.id.list);
        list.setEmptyView(view.findViewById(R.id.empty));
        getLoaderManager().initLoader(0, null, this).forceLoad();
    }

该片段当然实现了LoaderCallbacks,请在下面找到相关方法的实现:

The fragment of course implements LoaderCallbacks, find below the implementation of the relevant methods:

    @Override
public Loader<List<String>> onCreateLoader(int id, Bundle args) {

    logger.debug("Created loader");

    return new AsyncTaskLoader<List<String>>(getActivity()) {

        @Override
        public List<String> loadInBackground() {
             return getResults();
        }

    };
}

@Override
public void onLoadFinished(Loader<List<String>> loader,
        List<String> data) {
    logger.debug("loader finished");
    if (data != null) {
        adapter = new CustomListAdapter(getActivity());
        list.setAdapter(adapter);
        adapter.addAll(data);
    }
}

@Override
public void onLoaderReset(Loader<List<String>> loader) {

}

我遇到的问题是,对于第一页,所有内容均按预期工作,但是对于后续页面,我看到onCreateLoader调用已完成,AsyncTaskLoader的loadInBackground方法已调用,但onLoadFinished未调用且因此现在结果已交付.

The problem I am having is that for the first page everything works as expected, but for the subsequent pages, I see that the onCreateLoader call is done, the loadInBackground method of the AsyncTaskLoader is called, but the onLoadFinished is not invoked and therefore now results are delivered.

我正在使用Android支持库.

I am using the Android support library.

有人知道我在做什么错吗?

Does anyone know what am I doing wrong?

推荐答案

所以我刚遇到了这个问题,看来您的做法与我完全一样.看起来如果您的加载程序都使用相同的ID(在您的示例中为0),则它们将重新启动,并且如果分页加载的时间比另一页加载的时间长,则在分页的同时会重载.希望这仍然与遇到此问题的人有关!

So I just ran into this issue, and it seems you are doing it exactly as I did. Looks like if your loaders are all using the same id (in your example 0) they will restart and stomp all over each other while paging if one page load takes longer than another. Hope this can still be relevant to someone that run into this!

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

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