Observable(被观察者,发射器)发送数据:
just:发送单个的数据;
Observable.just("cui","chen","bo") .subscribe(new Consumer() { @Override public void accept(String s) throws Exception { Log.i(TAG, "accept: "+s); } });
Log:
04-27 22:44:05.988 7317-7317/com.ccb.pactera.rxdemo I/MainActivity: accept: cui
04-27 22:44:05.988 7317-7317/com.ccb.pactera.rxdemo I/MainActivity: accept: chen
04-27 22:44:05.988 7317-7317/com.ccb.pactera.rxdemo I/MainActivity: accept: bo
fromIterable:发送集合类型的数据;
Listdatas = Arrays.asList("H","W","Rx"); Observable.fromIterable(datas) .subscribe(new Observer () { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String value) { Log.i(TAG, "onNext: "+value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } });
Log:
04-27 22:44:05.988 7317-7317/com.ccb.pactera.rxdemo I/MainActivity: onNext: H
04-27 22:44:05.988 7317-7317/com.ccb.pactera.rxdemo I/MainActivity: onNext: W
04-27 22:44:05.988 7317-7317/com.ccb.pactera.rxdemo I/MainActivity: onNext: Rx
数据转换:
map:一对一的转换;
final Integer icon = R.mipmap.ic_launcher_round; //数据转换 Observable.create(new ObservableOnSubscribe() { @Override public void subscribe(ObservableEmitter e) throws Exception { e.onNext(icon); e.onComplete(); } }) .map(new Function , Drawable>() { @Override public Drawable apply(Integer integer) throws Exception { return getResources().getDrawable(integer); } }) .subscribe(new Observer () { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(Drawable value) { ImageView iv = new ImageView(MainActivity.this); iv.setImageDrawable(value); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } });
flatMap:一对多的转换;
public class Course { private String name; private int id; public Course(String name, int id) { this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } } public class Student { private String name; private ArrayListcourses; public Student(String name, ArrayList courses) { this.name = name; this.courses = courses; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ArrayList getCourses() { return courses; } public void setCourses(ArrayList courses) { this.courses = courses; } }
Course yuwen = new Course("语文", 1); Course shuxue = new Course("数学", 2); Course yingyu = new Course("英文", 3); Course lishi = new Course("历史", 4); Course zhengzhi = new Course("政治", 5); Course xila = new Course("希腊语", 6); ArrayListcourse1 = new ArrayList<>(); course1.add(yuwen); course1.add(shuxue); course1.add(yingyu); course1.add(lishi); course1.add(zhengzhi); course1.add(xila); Student zhangsan = new Student("zhangsan", course1); Observable.just(zhangsan) .flatMap(new Function , ObservableSource >() { @Override public ObservableSource apply(Student student) throws Exception { return Observable.fromIterable(student.getCourses()); } }) .subscribe(new Consumer () { @Override public void accept(Course course) throws Exception { Log.i(TAG, "accept: "+course.getName()); } });
Log:
04-27 23:12:50.084 11652-11652/? I/MainActivity: accept: 语文
04-27 23:12:50.084 11652-11652/? I/MainActivity: accept: 数学
04-27 23:12:50.084 11652-11652/? I/MainActivity: accept: 英文
04-27 23:12:50.084 11652-11652/? I/MainActivity: accept: 历史
04-27 23:12:50.084 11652-11652/? I/MainActivity: accept: 政治
04-27 23:12:50.084 11652-11652/? I/MainActivity: accept: 希腊语
线程切换:
默认:
Observable.just("CCB") .subscribe(new Consumer() { @Override public void accept(String s) throws Exception { Log.i(TAG, "accept: ---"+s+"--线程--"+Thread.currentThread()); } });
Log:
MainActivity: accept: ---CCB--线程--main (主线程);
添加subscribeOn(Schedulers.io());后;Log:MainActivity: accept: ---CCB--线程--RxCachedThreadScheduler-1 (子线程)
Observable.just("CCB") .subscribeOn(Schedulers.io()) .subscribe(new Consumer() { @Override public void accept(String s) throws Exception { Log.i(TAG, "accept: ---"+s+"--线程--"+Thread.currentThread()); } });
再添加: .observeOn(AndroidSchedulers.mainThread())后;log:MainActivity: accept: ---CCB--线程--main (主线程);
Observable.just("CCB") .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer() { @Override public void accept(String s) throws Exception { Log.i(TAG, "accept: ---"+s+"--线程--"+Thread.currentThread()); } });
这样我们就可以把一些耗时的操作(加载图片、网络请求)放在子线程,然后更新界面的时候再切换到主线程;各种线程间的切换只需要一行代码就可以搞定,而且链式调用,思路清晰;
OK!Rxjava的线程切换以及数据转换就说完了。