RxJava最核心的两个东西是Observables(被观察者,事件源)和Subscribers(订阅者)。Observables发出一系列事件,Subscribers处理这些事件。这里的事件可以是任何你感兴趣的东西,触摸事件,web接口调用返回的数据等等。android下也有RxAndroid,不过这个东西由于鄙人经验优先 ,运用的不好,还需多多撸代码
关于RxAndroid的github:https://github.com/ReactiveX/RxAndroid。
新建emRxAndroidStudy工程, 然后在build.grade的dependencies里添加:
- compile 'io.reactivex:rxandroid:1.1.0'
- compile 'io.reactivex:rxjava:1.1.0'
接着我们来试下RxAndroid了, 首先这里我们还是使用上次的注解的方式,把4个和Annotation相关的文件拷贝到工程,编写MainActivity代码如下:
- package com.jared.emrxandroidstudy;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import rx.Observable;
- import rx.Subscriber;
- @EMLayoutBinder(R.layout.activity_main)
- public class MainActivity extends BaseActivity {
- private static final String TAG = "MainActivity";
- private Subscriber<String> subscriber;
- private Observable<String> observable;
- @EMViewBinder(R.id.hello)
- private TextView mHello;
- @EMViewBinder(R.id.test1)
- private Button mTest1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- createSubscriber();
- }
- private void bindSubscriber() {
- observable.subscribe(subscriber);
- }
- private void createSubscriber() {
- subscriber = new Subscriber<String>() {
- @Override
- public void onCompleted() {
- Log.d(TAG, "onCompleted");
- }
- @Override
- public void onError(Throwable e) {
- e.printStackTrace();
- }
- @Override
- public void onNext(String t) {
- Log.d(TAG, "onNext");
- mHello.setText(t);
- }
- };
- }
- private String getHello() {
- return "Hello RxAndroid";
- }
- private String getHello1() {
- return "Hello RxAndroid 1";
- }
- @EMOnClickBinder({R.id.test1})
- public void myOnClick(View view) {
- switch (view.getId()) {
- case R.id.test1:
- createObservable();
- break;
- default:
- break;
- }
- }
- private void createObservable() {
- Log.d(TAG, "observable");
- observable = Observable.create(new Observable.OnSubscribe<String>() {
- @Override
- public void call(Subscriber<? super String> subscriber) {
- subscriber.onNext(getHello());
- subscriber.onCompleted();
- }
- });
- bindSubscriber();
- }
- }
布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.jared.emrxandroidstudy.MainActivity">
- <TextView
- android:id="@+id/hello"
- android:text="Hello World!"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/test1"
- android:text="Test"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textAllCaps="false"/>
- </LinearLayout>
这里创建了Observable,用来发送一字符串,然后创建了Subscriber,用来接收事件处理,然后把这两个绑定,按下按钮后,subscriber会调用onNext方法和onCompleted方法。
当然这里的createObservable可以通过just方法简化:
- private void createObservableByJust() {
- Log.d(TAG, "createObservable");
- observable = Observable.just(getHello());
- bindSubscriber();
- }
- private void createSubscriberByAction() {
- onNextAction = new Action1<String>() {
- @Override
- public void call(String s) {
- mHello.setText(s);
- }
- };
- }
这里通过Action1来实现,完全没有了subscriber了的感觉,接着修改绑定如下:
- private void bindSubscriber() {
- //observable.subscribe(subscriber);
- observable.subscribe(onNextAction);
- }
好了,接着我们来使用下操作符map,修改如下:
- private void createObservableByMap() {
- Log.d(TAG, "createObservableByMap");
- Observable.just(getHello()).map(new Func1<String, String>() {
- @Override
- public String call(String s) {
- return s + " by eastmoon";
- }
- }).subscribe(onNextAction);
- }
运行结果后原来字符串加上了by eastmoon了。其实map的功能就是在observable和subscribe之间可以对数据进行操作。