doOnNext操作符
這個操作符就是讓觀察者在接受事件之前干一點是,doOnNext()
也接受一個consumer
的接口
public class RxJavaDemo {
public static void main(String[] args) {
Observable<String> myObservableF = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
observableEmitter.onNext("test1");
observableEmitter.onNext("test2");
observableEmitter.onNext("test3");
observableEmitter.onComplete();
}
});
myObservableF.doOnNext(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println(s);
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println(s);
}
});
}
}
輸出的結(jié)果是
test1
test1
test2
test2
test3
test3
skip操作符
接受一個long
類型參數(shù)表示跳過的條數(shù)
public class RxJavaDemo {
public static void main(String[] args) {
Observable<String> myObservableF = Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
observableEmitter.onNext("test1");
observableEmitter.onNext("test2");
observableEmitter.onNext("test3");
observableEmitter.onComplete();
}
});
myObservableF.skip(1)
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println(s);
}
});
}
}
小結(jié)
操作符是RxJava的一大特色,RxJava的操作遠遠不止這些,以后有機會還會慢慢總結(jié)
操作符這個部分參考自http://www.reibang.com/p/0cd258eecf60