[英]Viewmodel Livedata doesn't update data observers
For example, I have 12 UpcomingGamesFragment
and each fragment has a different set of game data releasing a month, for example the first fragment of 12 will have video games releasing on January 2019. 例如,我有12个
UpcomingGamesFragment
,每个片段都有一个月发布的不同的游戏数据集,例如第一个12个片段将在2019年1月发布视频游戏。
In my app, there's a navigation drawer with a list of platforms (ps4, xbox, pc, etc.) and when the user picks his consoles by clicking on the check boxes and then closes the drawer layout, I want all the fragments to update accordingly. 在我的应用程序中,有一个带有平台列表的导航抽屉(ps4,xbox,pc等),当用户通过单击复选框选择控制台并关闭抽屉布局时,我希望所有片段都进行更新因此。 Like only show games releasing on these platforms.
就像只在这些平台上发布的表演游戏一样。 Retrieve and filter successfully works through a method in
UpcomingGamesFragment
called loadReleasesData()
通过
UpcomingGamesFragment
称为loadReleasesData()
的方法,成功检索和过滤
Now what I want is all the fragments to update when the navigation drawer gets closed, because my implementation doesn't work, please tell me what's wrong. 现在我想要的是在关闭导航抽屉时更新所有片段,因为我的实现无法正常工作,请告诉我出了什么问题。
Here's my ViewModel class: 这是我的ViewModel类:
public class ReleasesViewModel extends ViewModel {
private MutableLiveData<List<_Release>> upcomingReleases = new MutableLiveData<>();
public ReleasesViewModel() { }
public MutableLiveData<List<_Release>> getUpcomingReleases() {
return upcomingReleases;
}
}
And in my Filter drawer layout is in my MainActivity, and also I declare my ViewModel in my MainActivity: 在我的MainActivity中的Filter抽屉布局中,我也在MainActivity中声明ViewModel:
OnCreate (A lot of code omitted for clarity) OnCreate(为清晰起见,省略了大量代码)
mReleasesViewModel = ViewModelProviders.of(this).get(ReleasesViewModel.class);
My drawer layout on close: 我的抽屉布局关闭了:
@Override
public void onDrawerClosed(@NonNull View drawerView) {
// If change detected refresh!
if (!mCopyOfUserPlatforms.equals(SharedPrefManager.read(SharedPrefManager.PLATFORM_IDS, mDefaultPlatformsSet))) {
mReleasesViewModel.getUpcomingReleases().setValue(new ArrayList<_Release>());
}
}
And I pass the viewmodel livedata object to my 12 fragments when I initialize them in another fragment called the ViewPagerFragment
here's how: 当我在另一个称为
ViewPagerFragment
的片段ViewPagerFragment
其初始化时,将viewmodel livedata对象传递给我的12个片段,方法如下:
onCreateView: onCreateView:
// Get the ViewModel
ReleasesViewModel releasesViewModel = ((MainActivity)getActivity()).mReleasesViewModel;
for (int i = 0; i < 14; i++) {
UpcomingGamesFragment upcomingGamesFragment = new UpcomingGamesFragment();
upcomingGamesFragment.setLiveData(releasesViewModel); // HERE
mSectionsPagerAdapter.addFragment(upcomingGamesFragment, title, queryId);
}
This is my setLiveData() method in UpcomingGamesFragment: 这是UpcomingGamesFragment中的setLiveData()方法:
public void setLiveData(ReleasesViewModel releasesViewModel) {
releasesViewModel.getUpcomingReleases().observe(this, new android.arch.lifecycle.Observer<List<_Release>>() {
@Override
public void onChanged(@Nullable List<_Release> releases) {
Log.d(TAG, "Refreshing this baby boy...");
loadReleaseData(0);
}
});
}
How I know the livedata doesn't update all my fragments? 我怎么知道实时数据不会更新我所有的片段? It is because I have a log in my loadReleasesData method and it doesn't get printed in the Logcat and not to mention the fact it doesn't update the fragment(s).
这是因为我的loadReleasesData方法中有一个日志,并且它没有在Logcat中打印,更不用说它不会更新片段了。 Have a good day and bye!
祝你有美好的一天,再见! :)
:)
Your issue is not with LiveData
but with ViewModel
instance you're getting in ViewPagerFragment
: 您的问题不是
LiveData
而是ViewModel
实例,您正在使用ViewPagerFragment
:
use this in your onCreateView()
of fragment: 在片段的
onCreateView()
中使用它:
ReleasesViewModel releasesViewModel = ViewModelProviders.of(getActivity()).get(ReleasesViewModel.class);
instead of your code: 而不是您的代码:
// Get the ViewModel
ReleasesViewModel releasesViewModel = ((MainActivity)getActivity()).mReleasesViewModel;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.