1.添加:
//RX JAVA
implementation "io.reactivex.rxjava3:rxjava:3.0.0-RC6"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
2.编写此代码:
val taskObservable: Observable<Task> = Observable.fromIterable(DataSource.createTaskList())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
3.添加这些进口:
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function
import io.reactivex.schedulers.Schedulers
笔记 :
如果你使用的是 KOTLIN,那么最好使用 RX Kotlin!只需在 build.gradle 中添加两行:
//rxkotlin
implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"