【问题标题】:load more data on scroll to top in recyclerview在recyclerview中滚动到顶部加载更多数据
【发布时间】:2019-02-21 19:26:55
【问题描述】:

我试图在滚动到顶部 recyclerview 时加载更多数据,例如 Whatsapp 聊天。
我的适配器包含多个视图
我从 sqlite 加载几行以显示在 recyclerview 中,并在滚动顶部时加载更多数据。 当我加载更多数据时,应用程序崩溃了

02-20 22:57:01.324 6833-6833/ E/AndroidRuntime: FATAL EXCEPTION: main
Process: , PID: 6833
java.lang.NullPointerException
    at .adapters.PrivateChatAdapter$ViewHodler.setSenderMsg(PrivateChatAdapter.java:208)
    at .adapters.PrivateChatAdapter.onBindViewHolder(PrivateChatAdapter.java:124)
    at .adapters.PrivateChatAdapter.onBindViewHolder(PrivateChatAdapter.java:46)

线性布局管理器是linearLayoutManager.setStackFromEnd(true);

0     <---- i want to load the new data here
1
2    
3
4    
5
6

当我将新数据添加到数组列表时

arrayMsgs.add(0, new PrivateMsg.Builder(PrivateMsg.LOADING_MORE_MSG, 0, true).build());
privateChatAdapter.notifyItemInserted(0);

位置 0 的旧项重复

【问题讨论】:

  • 添加数据到列表后,可以调用privateChatAdapter.notifyDataSetChanged();通知适配器
  • 我已通知适配器已更改但问题在于视图
    在更新 arraylist 之前.. 位置零包含消息视图
    但是当我使用进度条更新位置零时并通知已更改的适配器他没有更新视图并保持消息视图

标签: android android-recyclerview


【解决方案1】:

我通过如下重置适配器解决了这个问题:

recyclerview.setAdapter(privateChatAdapter);

在数组列表中将数据添加到位置零后
可能是因为视图项存储在recyclerview的缓存中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多