I have an activity with a fragment. Inside that fragment there is a viewpager and inside that there is a list. Now once the user clicks on an item in the list the fragment should be replaced with another fragment and I need to pass some data like the list position and some other values linked to that. I can implement this by using interfaces, but as we are using rxjava so want to do it using rx... Don't want to implement the event bus or rxbus pattern right now. So how do I implement it using rxjava?
Asked
Active
Viewed 3,110 times
1 Answers
6
One way to do it:
/* inside whatever you mean by the list */
PublishSubject<Void> mClickSubject = PublishSubject.create(); //or use another type instead of Void if you need
/*...*/
item.setOnClickListener(v -> mClickSubject.onNext(null));
/*...*/
public Observable<Void> itemClicked() {
return mClickSubject;
}
/* pass your subject/observable all the way to the activity */
/* inside the activity */
private void setupSubscription() {
mCurrentFragment.listItemClicked()
.subscibe(/* switch fragment */);
}
Or another way is to have a singleton / static class holding a member PublishSubject
and push items through it. Doing it like this you won't need all the getters to pass the observable from the list to the activity.

AndroidEx
- 15,524
- 9
- 54
- 50
-
1Can you explain the second singleton method with a example if possible? – shivamDev31 Sep 10 '16 at 17:03
-
1@shivamDev I guess you're looking for something like this: https://stackoverflow.com/a/40836109/1101730 – Micer May 26 '17 at 08:40