介绍
RxJava 的官方介绍:RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
RxAndroid的官方介绍:RxJava bindings for Android.一般都结合Observable一起使用还有Retrofit,下面我就记录点简单的使用。RxJava的精妙之处就是Schedulers的使用。
添加依赖dependencies
//rxjava
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
//rxandroid
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
just的简单使用
观察者输出整型1、2、3
Observable.just(1,2,3)
.subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("onSubscribe","Subscribe");
}
@Override
public void onError(Throwable e) {
Log.d("onError",e.toString());
}
@Override
public void onNext(Integer integer) {
Log.d("onNext",integer+"");
}
@Override
public void onComplete() {
Log.d("onComplete","Complete");
}
});
输出结果:
04-10 04:12:01.421 14730-14730/com.song.rxjavademo D/onSubscribe: Subscribe
04-10 04:12:01.421 14730-14730/com.song.rxjavademo D/onNext: 1
04-10 04:12:01.421 14730-14730/com.song.rxjavademo D/onNext: 2
04-10 04:12:01.421 14730-14730/com.song.rxjavademo D/onNext: 3
04-10 04:12:01.421 14730-14730/com.song.rxjavademo D/onComplete: Complete
观察者输出字符串”string1”、”string2”、”string3”
Observable.just("string1","string2","string3")
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("onNext","onSubscribe");
}
@Override
public void onNext(String value) {
Log.d("onNext",value);
}
@Override
public void onError(Throwable e) {
Log.d("onError",e.toString());
}
@Override
public void onComplete() {
Log.d("onError","onComplete");
}
});
输出结果:
04-10 04:09:15.889 12236-12236/com.song.rxjavademo D/onNext: onSubscribe
04-10 04:09:15.889 12236-12236/com.song.rxjavademo D/onNext: string1
04-10 04:09:15.889 12236-12236/com.song.rxjavademo D/onNext: string2
04-10 04:09:15.889 12236-12236/com.song.rxjavademo D/onNext: string3
04-10 04:09:15.889 12236-12236/com.song.rxjavademo D/onError: onComplete
fromArray的使用
输出整型1、2、3
Integer[] items = {1,2,3};
Observable.fromArray(items).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("onSubscribe","onSubscribe");
}
@Override
public void onNext(Integer value) {
Log.d("onNext",""+value);
}
@Override
public void onError(Throwable e) {
Log.d("onError","onError");
}
@Override
public void onComplete() {
Log.d("onComplete","onComplete");
}
});
输出结果:
04-10 04:24:12.937 25593-25593/com.song.rxjavademo D/onSubscribe: onSubscribe
04-10 04:24:12.937 25593-25593/com.song.rxjavademo D/onNext: 1
04-10 04:24:12.937 25593-25593/com.song.rxjavademo D/onNext: 2
04-10 04:24:12.937 25593-25593/com.song.rxjavademo D/onNext: 3
04-10 04:24:12.937 25593-25593/com.song.rxjavademo D/onComplete: onComplete
repeat的简单使用
repeat(重复的次数)
简单循环三次,传进参数3,例如:repeat(3)
String[] items = {"string1","string2","string3"};
Observable.fromArray(items).repeat(3).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("onSubscribe","onSubscribe");
}
@Override
public void onNext(String value) {
Log.d("onNext",""+value);
}
@Override
public void onError(Throwable e) {
Log.d("onError","onError");
}
@Override
public void onComplete() {
Log.d("onComplete","onComplete");
}
});
输出结果:
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onSubscribe: onSubscribe
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string1
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string2
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string3
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string1
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string2
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string3
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string1
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string2
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onNext: string3
04-10 04:30:59.037 31616-31616/com.song.rxjavademo D/onComplete: onComplete
repeatWhen的简单使用
repeatWhen(Function) 传一个Function 每隔3秒钟执行一次
String[] items = {"string1", "string2", "string3"};
Observable.fromArray(items)
.repeatWhen(new Function<Observable<Object>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(Observable<Object> objectObservable) throws Exception {
return objectObservable.delay(3, TimeUnit.SECONDS);
}
})
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("onSubscribe", "onSubscribe");
}
@Override
public void onNext(String value) {
Log.d("onNext", "" + value);
}
@Override
public void onError(Throwable e) {
Log.d("onError", "onError");
}
@Override
public void onComplete() {
Log.d("onComplete", "onComplete");
}
});
输出结果:
04-10 05:00:07.709 14217-14217/com.song.rxjavademo D/onSubscribe: onSubscribe
04-10 05:00:07.713 14217-14217/com.song.rxjavademo D/onNext: string1
04-10 05:00:07.713 14217-14217/com.song.rxjavademo D/onNext: string2
04-10 05:00:07.713 14217-14217/com.song.rxjavademo D/onNext: string3
04-10 05:00:10.717 14217-14251/com.song.rxjavademo D/onNext: string1
04-10 05:00:10.717 14217-14251/com.song.rxjavademo D/onNext: string2
04-10 05:00:10.717 14217-14251/com.song.rxjavademo D/onNext: string3
......