首先在build.gradle中增加
implementation 'io.reactivex.rxjava2:rxjava:2.1.4'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
Observable几种创建方法
//使用Observable.just()创建被观察者
var observable2 = Observable.just("Hello", "World");
//使用Observable.from()创建被观察者
val words: IntArray = intArrayOf(1, 2, 3)
var observable3 = Observable.fromArray(words);
var list = ArrayList<String>();
list.add("Hellow");
list.add("Wrold");
var observable4 = Observable.fromArray(list);
简单的示例
Observable.create(ObservableOnSubscribe<Int?> { emitter ->
emitter.onNext(1)
emitter.onNext(2)
emitter.onNext(3)
emitter.onNext(4)
emitter.onComplete()
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<Int?> {
override fun onSubscribe(d: Disposable) {
println("onSubscribe=$d")
}
override fun onNext(t: Int) {
println("onNext=$t")
}
override fun onError(e: Throwable) {
println("onError=$e")
}
override fun onComplete() {
println("onComplete")
}
})
image
喜欢可以加Q群号:874826112,一起学习,成长,交流工作经验,
记得要点赞,评论哦😯;
更多相关内容请点击主页查看…
简书: https://www.jianshu.com/u/88db5f15770d
网友评论