![](/img/trans.png)
[英]How to refresh recyclerview adapter from other recyclerview adapter in same fragment
[英]How to refresh recyclerview adapter when data changes in other fragment?
第一個片段
private void initRecyclerView() {
Main.musicList = Main.songs.songs;
if ((Main.musicList != null) && (!Main.musicList.isEmpty())) {
// Connects the song list to an adapter
// (Creates several Layouts from the song list)
allSongsAdapter = new AllSongsAdapter(getActivity(), Main.musicList);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewSongs.setLayoutManager(linearLayoutManager);
recyclerViewSongs.setHasFixedSize(true);
recyclerViewSongs.setAdapter(allSongsAdapter);
}
}
在第一個片段中,我有一個recyclerview,該視圖顯示一個列表,其中包含在設備上找到的所有歌曲,並且我可以選擇刪除一首歌曲,該歌曲通過重新掃描設備上的所有歌曲來更改陣列列表Main.musicList中的數據。
因此,我的應用程序中的所有其他arraylist仍具有對該刪除歌曲的引用。
那么,當我刪除第一個片段中的項目時,如何在其他片段中的所有recyclerview適配器上調用notifySetDataChanged?
最簡單的方法是使用BroadcastReceiver
。 試試這個答案
在保存這些片段的Activity中使用ViewModel。
將歌曲列表包裝在LiveData對象中。 您的所有片段均可根據其生命周期進行觀察(片段處於OnResumed狀態時,列表更改時將自動通知該片段)
相關主題: https : //developer.android.com/topic/libraries/architecture/livedata https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54希望我能為您提供幫助。
您可以使用EventBus觸發該方法,通過該方法將通知reyclcer視圖適配器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.