第一个片段
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,它显示一个包含设备上所有歌曲的列表,我可以选择删除一首歌曲,通过重新扫描设备上的所有歌曲来改变我的arraylist Main.musicList中的数据。
所以我的应用程序中的所有其他arraylists仍然引用了已删除的歌曲。
那么当我删除第一个片段中的项目时,如何在其他片段中的所有recyclerview适配器上调用notifySetDataChanged?
简单的方法是使用BroadcastReceiver
。试试this的答案
相关主题:https://developer.android.com/topic/libraries/architecture/livedata https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54希望我能帮到你。
您可以使用EventBus触发方法,通过该方法将通知reyclcer视图适配器