RxAndroid: Reactive Extensions for Android
Android specific bindings for RxJava.
Android特定的RxJava绑定。
This module adds the minimum classes to RxJava that make writing reactive components in Android applications easy and hassle-free. More specifically, it provides a Scheduler
that schedules on the main thread or any given Looper
.
此模块将最少的类添加到RxJava,使得在Android应用程序中编写响应使式组件简单、无忧。 具体说,它提供了在主线程或任何给定Looper
上调度的Scheduler
。
Communication
联系
Since RxAndroid is part of the RxJava family the communication channels are similar:
由于RxAndroid是RxJava家族的一部分,因此联络途径类似:
- Google Group: RxJava
- Twitter: @RxJava
- *: rx-android
- GitHub Issues
Binaries
二进制文件
compile 'io.reactivex:rxandroid:1.2.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// 因为RxAndroid版本很少,所以明确建议您使用RxJava的最新版本进行错误修复和新功能开发。
compile 'io.reactivex:rxjava:1.1.6'
- RxAndroid: maven-central
- RxJava: maven-central
Additional binaries and dependency information for can be found at http://search.maven.org.
有关其他二进制文件和依赖项信息,请访问http://search.maven.org。
Build
编译
To build:
编译:
$ git clone git@github.com:ReactiveX/RxAndroid.git
$ cd RxAndroid/
$ ./gradlew build
Further details on building can be found on the RxJava Getting Started page of the wiki.
关于编译的更多细节可以在RxJava的wiki入门页面找到。
Sample usage
示例用法
A sample project which provides runnable code examples that demonstrate uses of the classes in this project is available in the sample-app/
folder.
在sample-app /
文件夹中提供了一个示例项目,它提供了可运行的代码示例,演示了此项目中类的使用。
Observing on the main thread
观察主线程
One of the most common operations when dealing with asynchronous tasks on Android is to observe the task’s result or outcome on the main thread. Using vanilla Android, this would typically be accomplished with an AsyncTask
. With RxJava instead you would declare your Observable
to be observed on the main thread:
在Android上处理异步任务时最常见的操作之一是在主线程上观察任务的结果。 使用原生Android,我们通常使用AsyncTask
来实现。 使用RxJava,你可以声明你的Observable
在主线程上进行观察:
Observable.just("one", "two", "three", "four", "five")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* an Observer */);
This will execute the Observable
on a new thread, and emit results through onNext
on the main thread.
这将在新线程上执行Observable
,并通过onNext
在主线程上发出结果。
Observing on arbitrary loopers
观察任意的Looper
The previous sample is merely a specialization of a more general concept: binding asynchronous communication to an Android message loop, or Looper
. In order to observe an Observable
on an arbitrary Looper
, create an associated Scheduler
by calling AndroidSchedulers.from
:
前面仅仅是通用概念下特定化的一个例子:将异步通信绑定到Android消息循环或Looper
。 为了观察任意Looper
的Observable
,通过调用AndroidSchedulers.from
创建一个相关联的Scheduler
:
Looper backgroundLooper = // ...
Observable.just("one", "two", "three", "four", "five")
.observeOn(AndroidSchedulers.from(backgroundLooper))
.subscribe(/* an Observer */)
This will execute the Observable on a new thread and emit results through onNext
on whatever thread is running backgroundLooper
.
这将在新线程上执行Observable,并通过onNext在任何运行backgroundLooper的线程上发出结果。
Bugs and Feedback
错误和反馈
For bugs, feature requests, and discussion please use GitHub Issues. For general usage questions please use the mailing list or *.
对于错误、功能需求和讨论,请使用GitHub Issues。 对于一般的使用问题,请使用邮件列表或*。
LICENSE
许可
Copyright 2015 The RxAndroid authors
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
项目地址:https://github.com/ReactiveX/RxAndroid.git
文章地址:https://github.com/ReactiveX/RxAndroid/blob/1.x/README.md
译:刘炳昭 http://blog.csdn.net/liubingzhao