【问题标题】:How to avoid jerk when notifyDataSetChanged method of adapter is called?调用适配器的 notifyDataSetChanged 方法时如何避免混蛋?
【发布时间】: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


【解决方案1】:

由于 ListView 已经过时并且已经过时,现在您可以使用 Recyclerview 而不是 ListView,这里是帮助你转换你的链接 ListView 到 RecyclerView。 RecyclerView 有许多方法可以添加您的自定义动画,并且在添加/删除项目时也没有任何抖动。

Replacing ListView with RecyclerView

对于默认动画,您可以使用此链接:

RecyclerView Animations – Add & Remove Items

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2014-12-14
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 2013-10-15
    • 1970-01-01
    相关资源
    最近更新 更多