Android authorities defined, Observables and Observers in terms of RxAndrod, There are two basic and very important items in reactive programming, Observables and Observers. Observables publish values, while Observers subscribe to Observables, watching them and reacting when an Observable publishes a value.
In simpler terms:
An Observable performs some action, and publishes the result.
An Observer waits and watches the Observable, and reacts whenever the Observable publishes results.
There are three different changes that can occur on an Observable that the Observer reacts to. These are:
- Publishing a value
- Throwing an error
- Completed publishing all values
A class that implements the Observer interface must provide methods for each of the three changes above:
- An onNext() method that the Observable calls whenever it wishes to publish a new value
- An onError() method that’s called exactly once, when an error occurs on the Observable.
- An onCompleted() method that’s called exactly once, when the Observable completes execution.
Example: Display list of data using RxAndroid in to RecyclerView
First you have to import the dependencies latest version of RxAndrod, RxJava and other like recyclerview, in to the build.gradle on app level inside the dependencies section.
Layout:
RecyclerView is simply A flexible view for providing a limited window into a large data set.
activity_main.xml
and custom list item layout
Now, Obserable implement on MainActivity,
and Now implement RecyclerView Adapter
Output looks like:
Download full code from github: RxAndroid_RecyclerView_Example
Happy Coding!!!