【发布时间】:2015-09-17 20:38:00
【问题描述】:
我正在尝试在我的 listView 中添加分页。
我在自定义 BaseAdapter 中添加了一个方法,该方法在滚动到列表中的第一项时将下一页结果添加到列表顶部。
public void addEntriesToTop(List<ChatModel> entries) {
// Add entries to the top of the list
this.chatMessages.addAll(0, entries);
notifyDataSetChanged();
}
所以如果我必须在 ListView 顶部添加下一页,我会在我的活动中使用这样的方法。
//result contain new items to be added to top of list
adapter.addEntriesToTop(result);
//so that list scroll sets to last visible message to user
final int index = result.size();
listView.post(new Runnable() {
@Override
public void run() {
//go to user's last scroll position
messagesContainer.setSelectionFromTop(index, 0);
}
});
此方法运行良好,并保留了用户的最后滚动位置。但它有一个问题,当我调用 addEntriesToTop() 时,它会将 ListView 滚动到底部(因为 notifyDataSetChanged() ),然后当我调用 setSelectionFromTop 时,它会滚动到用户的最后一个位置。在这个过渡中,有一个小混蛋。
请帮助我顺利过渡。
谢谢
【问题讨论】:
-
我知道你会在 listview 中寻找解决方案,但尝试使用 recyclerview 它有诸如 notifiyiemrangeinserted(...) 之类的方法,recyclerview 中不会出现“混蛋”
-
我希望我可以将我的 listView 更改为 recycleView,但我不能。在 listView 中提出建议?
标签: android performance listview android-listview