首页 > 解决方案 > 片段附加和分离后 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 来刷新,然后项目就消失了。
如何解决这个问题?

标签: androidandroid-fragmentsandroid-recyclerview

解决方案


您需要从已传递给 recyclerView 适配器的列表中删除条目,然后像这样在适配器上调用 notify -

    list.remove(position); // to remove the item from the list
    recyclerAdapter.notifyItemRemoved(position); // to notify the adapter

其中位置是列表中要删除的项目的索引。在此之后,该项目将从 recyclerView 中删除并且不再可见。


推荐阅读