我正在使用可观察的模式来更新我的12个UpcomingGamesFragment
片段
这是我的UpcomingGamesFragment
类,其中包含我调用的更新方法
public class UpcomingGamesFragment extends Fragment implements Observer {
public void loadReleaseData(final int refresh) {
if (mDatabaseLoading == null) {
Log.d(TAG, "Fragment filter " + mFilter + " [fragment is null]");
return;
} else {
Log.d(TAG, "Updating fragment: " + mFilter);
}
if (AppUtil.doesInternetWork(getActivity())) {
// Viewpager does not load all fragments in memory, contents will be null
mDatabaseLoading.setVisibility(View.VISIBLE);
mUpcomingList.setVisibility(View.VISIBLE);
mEmptyTextInfo.setVisibility(View.GONE);
noInternetTxt.setVisibility(View.GONE);
mUpcomingPageLayout.setClickable(false);
Query query = mDatabaseRef.child("releases")
.child(mRegion)
.child("data")
.orderByChild("m_y")
.equalTo(mFilter);
if (mFilter.equals("future")) {
// Get all future releases
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.getInstance().get(Calendar.MONTH));
calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR) + 1);
query = mDatabaseRef.child("releases")
.child(mRegion)
.child("data")
.orderByChild("date")
.startAt(calendar.getTimeInMillis());
}
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChildren()) {
if (refresh == 1) {
// Toast.makeText(getContext(), "adasfsdz", Toast.LENGTH_SHORT).show();
resetList();
Toast.makeText(getContext(), "Refreshing list...", Toast.LENGTH_SHORT).show();
}
for (DataSnapshot data : dataSnapshot.getChildren()) {
_Release release = data.getValue(_Release.class);
if (release != null) {
if (mSelectedPlatforms.isEmpty()) {
// No platform filter set add all releases!
if (release.getGame() != null) {
mUpcomingReleases.add(release);
}
} else {
if (release.getGame() != null) {
// If current platform is in ours shared pref of platforms
if (!Collections.disjoint(release.getPlatform(), mSelectedPlatforms)) {
mUpcomingReleases.add(release);
}
}
}
}
mUpcomingGamesAdapter.notifyDataSetChanged();
}
// Check if list empty, if it is no releases matching platforms filter
if (mUpcomingReleases.isEmpty()) {
mEmptyTextInfo.setText("No releases found matching your platforms");
mEmptyTextInfo.setVisibility(View.VISIBLE);
}
} else {
mEmptyTextInfo.setText("No releases found for this month");
mEmptyTextInfo.setVisibility(View.VISIBLE);
}
mDatabaseLoading.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
mDatabaseLoading.setVisibility(View.GONE);
}
});
}
}
@Override
public void update() {
loadReleaseData(0);
}
}
我还有另一个片段UpcomingViewPagerFragment
,其中包含viewpager
,其中包含12个片段,这个片段是可观察到的,这里是:
public class UpcomingViewPagerFragment extends Fragment implements Observable {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_upcoming_view_pager, container, false);
// Viewpager
mViewPager = view.findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(3);
// Get TabLayout from Main
TabLayout tabLayout = ((MainActivity)getActivity()).mTabLayout;
tabLayout.setupWithViewPager(mViewPager);
mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity().getSupportFragmentManager());
Calendar calendar = Calendar.getInstance();
SimpleDateFormat monthFormatter = new SimpleDateFormat("MMMM");
// Add 11 months [Maximum] to show the releases for the next year [14 fragments in total]
// First page recently released [-1] gets the last month of the current month
calendar.add(Calendar.MONTH, -1);
for (int i = 0; i < 14; i++) {
String title = monthFormatter.format(calendar.getTime()) + " " + calendar.get(Calendar.YEAR);
String queryId = (calendar.get(Calendar.MONTH) + 1) + "_" + calendar.get(Calendar.YEAR);
if (i == 13) {
// the last one
queryId = "future";
title = "Near future";
}
UpcomingGamesFragment upcomingGamesFragment = new UpcomingGamesFragment();
mSectionsPagerAdapter.addFragment(upcomingGamesFragment, title, queryId);
calendar.add(Calendar.MONTH, 1);
// Log.d(TAG, "Added fragment: " + title + " : " + queryId);
// Adding observers
this.addObserver(upcomingGamesFragment);
}
mViewPager.setAdapter(mSectionsPagerAdapter);
// Because the first page will always be the recently released
mViewPager.setCurrentItem(1, true);
return view;
}
@Override
public void addObserver(Observer o) {
mObserversList.add(o);
}
@Override
public void deleteObserver(Observer o) {
mObserversList.remove(o);
}
@Override
public void notifyObservers() {
for (Observer observer: mObserversList) {
observer.update();
}
}
}
为清晰起见,省略了一些代码,例如两个片段的默认构造函数
然后,当我调用更新notifyObervers()
中所有12个片段MainActivity
的方法时,我总是得到组件mDatabaseLoading
是null
(请参阅{{1 }}方法),这样可见的片段就不会得到更新,知道为什么组件总是总是loadReleasData()
吗?