目前我有一个ListView实现无尽的列表加载,因为它会加载X数量的项目,然后当用户滚动到底部,它执行一个AsyncTask调用来加载更多项目,然后将它们加载到列表中并进行更新。目前这一切都是有效的,但问题是为了让它工作,它必须非常低效地完成,直到列表变得越来越长,加载新项目需要更长的时间,因为整个列表是“替换”每次,这显然是不可取的。这里是我目前从我的AsyncTask列表更新它得到一个成功的响应后:列表适配器不更新新项目
//this first line is what makes the loading so slow, but without it the list returns an empty set when calling notifyDataSetChanged() on the ListAdapter
TopPostFragment.postList = new ArrayList<Post>(TopPostFragment.postList);
TopPostFragment.postList.addAll(result); //result is the new items that were received from the AsyncTask
TopPostFragment.updateList();
显然,这第一行是完全没有必要的,但由于某些原因,如果我删除列表中不正确更新该适配器将更新为空列表。我updateList()在TopPostFragment方法如下:
public static void updateList()
{
if(listAdapter != null)
{
listAdapter.clear();
listAdapter.addAll(postList);
listAdapter.notifyDataSetChanged();
}
}
我有一种感觉,这个问题可能以某种方式来自我如何处理它在我的listAdapter干,所以这里是我的构造为ListAdapter对我是如何处理它:
List<Post> postList = null;
public PostListAdapter(Context context, int layoutResourceId, List<Post> list, int whichList, int currentFeedID) {
super(context, layoutResourceId, list);
postList = list;
}
我已经从这里的所有代码中删除了与该问题无关的代码行。如果我将它复制到一个全新的列表中,它只是在更新列表的位置上做错了什么?
看到需求光标这两个学家一个高效:https://gist.github.com/pskink/b21c932f405740011144和测试的ContentProvider https://gist.github.com/pskink/9ab862cdd0d6b0281ab4 – pskink