AsynLoaderTask and ViewPager

放肆的年华 提交于 2019-12-07 14:26:29

问题


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();
    }

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) {

}

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.

I am using the Android support library.

Does anyone know what am I doing wrong?


回答1:


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!



来源:https://stackoverflow.com/questions/17885053/asynloadertask-and-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!