rxJava rxandroid 学习

学习地址 很全面:

http://blog.csdn.net/meegomeego/article/details/49155989

final String[] words = {"Hello", "Hi", "Aloha"};

Observable.from(words).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Logger.e("--->onCompleted");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                Logger.e("--->onNext=="+s);
            }
        });

//map() 方法将参数中的 String 对象转换成一个 Bitmap 对象后返回(事件对象的直接变换)

Observable.just("xx").map(new Func1<String, Bitmap>() {
            @Override
            public Bitmap call(String s) {
                return null;
            }
        }).subscribe(new Action1<Bitmap>() {
            @Override
            public void call(Bitmap bitmap) {

            }
        });

//flatMap 返回Observable 对象 然后进行平铺下去
ArrayList<PreSenterEntity> lists=new ArrayList<PreSenterEntity>();
        for (int i=0;i<10;i++){
            Owener owner= new Owener("name"+i,i);
            lists.add(new PreSenterEntity(i,"name"+i,owner));
        }

        Observable.from(lists).flatMap(new Func1<PreSenterEntity, Observable<Owener>>() {
            @Override
            public Observable<Owener> call(PreSenterEntity preSenterEntity) {
                return Observable.from((Iterable<? extends Owener>) preSenterEntity.owner);
            }
        }).subscribe(new Subscriber<Owener>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Owener owener) {
                Logger.e(owener.id+"xxxx"+owener.login);

            }
        });

 

  • subscribeOn(): 指定 subscribe() 所发生的线程,即 Observable.OnSubscribe 被激活时所处的线程。或者叫做事件产生的线程。
  • observeOn(): 指定 Subscriber 所运行在的线程。或者叫做事件消费的线程。
int drawableRes = ...;
ImageView imageView = ...;
Observable.create(new OnSubscribe<Drawable>() {
    @Override
    public void call(Subscriber<? super Drawable> subscriber) {
        Drawable drawable = getTheme().getDrawable(drawableRes));
        subscriber.onNext(drawable);
        subscriber.onCompleted();
    }
})
.subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
.observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
.subscribe(new Observer<Drawable>() {
    @Override
    public void onNext(Drawable drawable) {
        imageView.setImageDrawable(drawable);
    }

    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
        Toast.makeText(activity, "Error!", Toast.LENGTH_SHORT).show();
    }
});

 创建Observable

Observable.create(new Observable.OnSubscribe<Drawable>() {
            @Override
            public void call(Subscriber<? super Drawable> subscriber) {
                Drawable drawable=null;

                subscriber.onNext(drawable);

                subscriber.onCompleted();

            }
        }).subscribe(new Observer<Drawable>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Drawable drawable) {

            }
        });

 

posted on 2016-02-23 16:17  yujian_bcq  阅读(308)  评论(0编辑  收藏  举报

导航