android - 片段附加和分离后 RecyclerView 项目未更改
问题描述
我Fragment
在应用程序中有 A、B。我已经Recyclerview
使用 API 调用在 Fragment A 上进行了设置。我deleteEntry()
在 Fragment B 中有一个方法,因此从 API 数据中删除了一项,删除该项目后我来到 Fragment A。
这是 API 调用deleteEntry
后移动到片段 A 的代码
CurrentStatus status=new CurrentStatus();
Busy table=new Busy();
tx=fm.beginTransaction();
tx.replace(R.id.frame,new ChooseTab());
tx.detach(status);
tx.attach(status);
tx.detach(table);
tx.attach(table);
tx.commit();
但是在我转到 Fragment A 之后,即使 API 在替换 Fragment 时再次调用以显示修改后的 API 数据,RecyclerView 项目仍然可见。我需要再次手动调用相同的 API 来刷新,然后项目就消失了。
如何解决这个问题?
解决方案
您需要从已传递给 recyclerView 适配器的列表中删除条目,然后像这样在适配器上调用 notify -
list.remove(position); // to remove the item from the list
recyclerAdapter.notifyItemRemoved(position); // to notify the adapter
其中位置是列表中要删除的项目的索引。在此之后,该项目将从 recyclerView 中删除并且不再可见。
推荐阅读
- java - Spring Boot 中的自定义验证器
- javascript - 使用 for 循环包装异步操作是否会使这些操作同步发生?
- google-cloud-platform - 将 com.google.datastore.v1.Entity 与 com.google.cloud.datastore.Entity 相互转换
- extjs - 将额外的请求参数传递给 model.save() 方法是通过 URL 查询参数发送的
- javascript - 无法使用控制器类中的方法
- python - 如何将 CSV 文件中的数据提取到我的 RASA 聊天机器人中?
- python-3.x - 如何动态导入包含相对导入子模块的模块?
- javascript - 将 eventClick 的事件 id 发送到 views.py Django
- reactjs - 动态显示时间线数据 react-google-charts
- python - 如何通过 PySeal 库加密数据框列