[英]RecyclerView items not changed after Fragment attach and detach
我在應用程序中有Fragment
A,B。我使用API調用在片段A上設置了Recyclerview
。我在片段B中有一個方法deleteEntry()
,由於其中一項從API數據中刪除,刪除后我來到了片段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進行刷新,然后Item消失了。
如何解決呢?
您需要從傳遞到recyclerView適配器的列表中刪除條目,然后像這樣在適配器上調用notify-
list.remove(position); // to remove the item from the list
recyclerAdapter.notifyItemRemoved(position); // to notify the adapter
其中position是要刪除列表中項目的索引。 此后,該項目將從recyclerView中刪除,不再可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.