• 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) {
    
                }
            });
  • 相关阅读:
    微信开发生成带参数的二维码的讲解
    C#利用最新版的WPS实现导入导出
    【模版消息】C#推送微信模版消息(Senparc.Weixin.MP.dll)
    Photoshop的辅助线
    Newtonsoft.Json 两个Attribute含义
    VUE2.0 饿了吗视频学习笔记(二):新版本添加路由和显示Header
    VUE2.0 饿了吗视频学习笔记(一):VUE示例data.json
    Windows句柄数限制
    The CLI moved into a separate package: webpack-cli.解决办法
    Winform窗体设计工具源码
  • 原文地址:https://www.cnblogs.com/yujian-bcq/p/5210326.html
Copyright © 2020-2023  润新知