android - Recyclerview 刷新项目甚至无需调用 notifyDataSetChanged()
问题描述
让我解释一下发生了什么。我有一个片段,在它的 onCreateView() 中我将适配器设置为 recyclerview 没有任何项目..
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
downloadsList = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
downloadsRecyclerview.setLayoutManager(linearLayoutManager);
downloadsList.clear();
downloadAdapter = new DownloadAdapter(context, downloadsList);
downloadsRecyclerview.setAdapter(downloadAdapter);
}
现在奇怪的部分是每当我将项目添加到downloadsList
(基本上是一个 ArrayList )时,我的 recyclerview 就会使用 downloadsList 上可用的内容进行更新。我什至没有调用 notifyDataSetChanged() 或再次设置新适配器...任何帮助..
解决方案
如果您不想重新加载整个RecyclerView
使用notifyDataSetChanged()
然后简单添加notifyItemInserted()
当您添加价值时downloadsList
,只需调用
downloadAdapter.notifyItemInserted(downloadsList.size()-1)
推荐阅读
- react-native - 在 MapView 标记周围添加阴影
- python - 'ascii' 编解码器在使用 python pandas 和 xlsxwriter 将表从 OracleDB 导出到 excel 时无法解码字节 0xc2 序数不在范围内(128)
- arrays - Google Apps 脚本 - 随机化下拉列表 - 选择索引
- java - 运行 docker build -t 时出现 Docker 问题
- javascript - 复杂解构:数组的第一个索引内的对象在另一个对象内
- python - 调试模式下的 Flask Restful 自定义错误处理程序
- html - 为什么我的下拉菜单不起作用?(HTML&CSS)
- r - 如何(重新)配对现有数据集以匹配最高可能的相关性
- java - 如何获取字符串的数值(整数或浮点数)?
- c++ - 为什么在从 double 转换为 float 时支撑初始化不会引发缩小错误?