Last active
June 21, 2024 19:11
-
-
Save alexfu/5797429 to your computer and use it in GitHub Desktop.
Observer pattern for notifying Fragments of a ViewPager to update their views. This will update the current Fragment, as well as the off screen Fragments that are retained.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FragmentObserver extends Observable { | |
@Override | |
public void notifyObservers() { | |
setChanged(); // Set the changed flag to true, otherwise observers won't be notified. | |
super.notifyObservers(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyActivity extends Activity { | |
private MyAdapter mPagerAdapter; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
setContentView(R.layout.my_activity); | |
ViewPager pager = (ViewPager) findViewById(R.id.pager); | |
mPagerAdapter = new MyAdapter(); | |
pager.setAdapter(mPagerAdapter); | |
} | |
private void updateFragments() { | |
mPagerAdapter.updateFragments(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyAdapter extends FragmentPagerAdapter { | |
private Observable mObservers = new FragmentObserver(); | |
public MyAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public Fragment getItem(int position) { | |
mObservers.deleteObservers(); // Clear existing observers. | |
Fragment fragment = new MyFragment(); | |
if(fragment instanceof Observer) | |
mObservers.addObserver((Observer) fragment); | |
return fragment; | |
} | |
public void updateFragments() { | |
mObservers.notifyObservers(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyFragment extends Fragment implements Observer { | |
/* Fragment related stuff... */ | |
@Override | |
public void update(Observable observable, Object data) { | |
View root = getView(); | |
// Update your views here. | |
} | |
} |
Work fine! For notify all fragments remove
mObservers.deleteObservers
. Work great for me.
For notify with object change your implementation of FragmentObserver:public class FragmentObserver extends Observable { @Override public void notifyObservers(object: Object) { setChanged(); // Set the changed flag to true, otherwise observers won't be notified. super.notifyObservers(object); } }
In Kotlin:
class FragmentObserver : Observable() {
override fun notifyObservers(arg: Any?) {
setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
super.notifyObservers(arg)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Man! You Save my Day