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.
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. Like only show games releasing on these platforms. Retrieve and filter successfully works through a method in UpcomingGamesFragment
called 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:
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:
OnCreate (A lot of code omitted for clarity)
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:
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:
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). Have a good day and bye! :)
Your issue is not with LiveData
but with ViewModel
instance you're getting in ViewPagerFragment
:
use this in your onCreateView()
of fragment:
ReleasesViewModel releasesViewModel = ViewModelProviders.of(getActivity()).get(ReleasesViewModel.class);
instead of your code:
// Get the ViewModel
ReleasesViewModel releasesViewModel = ((MainActivity)getActivity()).mReleasesViewModel;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.