Suggest that you first understand the observer pattern.
Pre-work: Introduction of RX Packages
Build.gradle script inside the
dependencies{
Compile ' io.reactivex:rxjava:1.1.0 '
Compile ' io.reactivex:rxandroid:1.1.0 '
}
Required Classes Observable subscriber
The first type of notation:
1. Create the observed person
observable<string> Observable = observable.create (new observable.onsubscribe<string> () {
@Override
public void call (SUBSCRIBER<? Super String> Subscriber) {
if (!subscriber.isunsubscribed ()) {
Subscriber.onnext ("Hello");
Subscriber.onnext ("Hi");
Subscriber.onnext ("World");
Subscriber.onnext ("OK");
Subscriber.oncompleted (); End
}
}
});
2. Define the viewer
subscriber<string> subscriber = new subscriber<string> () {
@Override
public void oncompleted () {
LOG.E (TAG, "oncompleted");
}
@Override
public void OnError (Throwable e) {
LOG.E (TAG, "onError");
}
@Override
public void OnNext (String str) {
LOG.E (TAG, str);
}
};
3. Association of Observed persons
Observable.subscribe (subscriber);
The second way:
Observable.create (New observable.onsubscribe<integer> () {
@Override
public void call (SUBSCRIBER<? Super Integer> Subscriber) {
if (!subscriber.isunsubscribed ()) {
for (int i = 0; i < i++) {
Subscriber.onnext (i);
}
Subscriber.oncompleted ();
}
}
}). Subscribe (New subscriber<integer> () {
@Override
public void oncompleted () {
LOG.E (TAG, "oncompleted");
}
@Override
public void OnError (Throwable e) {
LOG.E (TAG, "onError");
}
@Override
public void OnNext (integer integer) {
LOG.E (TAG, string.valueof (integer));
}
});
}
RX (i)