简体   繁体   English

当其他片段中的数据更改时,如何刷新recyclerview适配器?

[英]How to refresh recyclerview adapter when data changes in other fragment?

First 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);
   } 
}

In the first fragment i have a recyclerview which displays a list with all songs found on the device and i have an option to delete a song which changes the data in my arraylist Main.musicList by rescanning all songs on the device. 在第一个片段中,我有一个recyclerview,该视图显示一个列表,其中包含在设备上找到的所有歌曲,并且我可以选择删除一首歌曲,该歌曲通过重新扫描设备上的所有歌曲来更改阵列列表Main.musicList中的数据。

So all my other arraylists in my app still have a reference to that deleted song. 因此,我的应用程序中的所有其他arraylist仍具有对该删除歌曲的引用。

So how can i call notifySetDataChanged on all recyclerview adapters in other fragments when i delete an item in the first fragment? 那么,当我删除第一个片段中的项目时,如何在其他片段中的所有recyclerview适配器上调用notifySetDataChanged?

The easy way will be to use BroadcastReceiver . 最简单的方法是使用BroadcastReceiver try this answer 试试这个答案

  1. Use ViewModel in your Activity which holds these fragments. 在保存这些片段的Activity中使用ViewModel。

  2. Wrap your song list in LiveData object. 将歌曲列表包装在LiveData对象中。 This can be observed by all of your fragments depending on their lifecycle (when the fragment is in OnResumed state, it will be automatically notified when your list has changed) 您的所有片段均可根据其生命周期进行观察(片段处于OnResumed状态时,列表更改时将自动通知该片段)

  3. In related fragment start to observe LiveData and on change update your adapter. 在相关片段中,开始观察LiveData并在更改时更新您的适配器。

Related topics: https://developer.android.com/topic/libraries/architecture/livedata https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54 Hope I could help you. 相关主题: 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.

相关问题 如何从同一片段中的其他 recyclerview 适配器刷新 recyclerview 适配器 - How to refresh recyclerview adapter from other recyclerview adapter in same fragment 从其他应用程序导航时如何通知 recyclerview 适配器更改 - How to notify recyclerview adapter changes when navigating from other app 在另一个片段中更改数据时如何刷新一个片段中的RecyclerView - How to refresh RecyclerView in one fragment when data changed in another fragment 当其他片段更改第一个片段模型时刷新片段 - Refresh fragment when other fragment changes the first's fragment model 如何将数据从 recyclerview 适配器传递或发送到片段 - How to pass or send data from recyclerview adapter to fragment RecyclerView中的数据未从片段传递到适配器 - Data in RecyclerView not being passed from Fragment to Adapter 从另一个片段/保存recyclerview位置返回后,如何防止片段刷新recyclerview - How to prevent fragment to refresh recyclerview when after back from another fragment / save recyclerview position 当我使用recyclerview和适配器启动片段时出错 - Error when i start fragment with recyclerview and adapter 如何在片段中使用 recyclerview 适配器布局对象? - How to Use recyclerview adapter layout objects in fragment? 如何从 recyclerview 适配器关闭对话框片段 - How to dismiss a dialog fragment from a recyclerview adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM