Rxjava三种方法

package com.bwie.categorydemo.rxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.bwie.categorydemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * 被观察者Observable的实现
 */
public class RXObservableActivity extends AppCompatActivity {
    private static final String TAG = "RXObservableActivity";

    @BindView(R.id.btn_observable_01)
    Button btnObservable01;
    @BindView(R.id.btn_observable_02)
    Button btnObservable02;
    @BindView(R.id.btn_observable_03)
    Button btnObservable03;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rxobservable);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_observable_01, R.id.btn_observable_02,
            R.id.btn_observable_03})
    public void onObservableCreate(View v) {
        switch (v.getId()) {
            case R.id.btn_observable_01:
                // 第一种创建方式
                firstCreate();
                break;
            case R.id.btn_observable_02:
                secondCreate();
                break;
            case R.id.btn_observable_03:
                thirdCreate();
                break;
        }
    }

    /**
     * 第三种创建方式
     */
    private void thirdCreate() {
        // 常用的一种调用方式,链式调用
        Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("1");
                subscriber.onNext("abc");
                subscriber.onNext("aaaaa");
                subscriber.onNext("5");
            }
        }).flatMap(new Func1<String, Observable<String>>() {
            @Override
            public Observable<String> call(String s) {
                return null;
            }
        }).map(new Func1<String, String>() {
            @Override
            public String call(String s) {
                return null;
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, "call: " + s);
            }
        });
    }

    // 第二种创建方式
    private void secondCreate() {
        String[] str = {"Java", "PHP", ".NET", "Android", "iOS"};
//        List<String> list = Arrays.asList(str);
        // from添加的是数组或者集合
        Observable<String> from = Observable.from(str);
        from.subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.i(TAG, "call: " + s);
            }
        });
    }

    // 第一种创建方式
    private void firstCreate() {
        Observable<Integer> just = Observable.just(1, 2, 3, 4, 5, 6, 7, 8);

        just.subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                Log.i(TAG, "call: " + integer);
            }
        });
    }

}
阅读更多

没有更多推荐了,返回首页