RxJava操作符
- map() 操作符就是用于變換Observable對象的浮声,map操作符返回一個Observable對象,這樣就可以實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用赋铝,在一個Observable對象上多次使用map操作符烘挫,最終將最簡潔的數(shù)據(jù)傳遞給Subscriber對象
- from() 接收一個集合作為輸入,然后每次輸出一個元素給subscriber
- flatmap() Observable.flatMap()接收一個Observable的輸出作為輸入柬甥,同時(shí)輸出另外一個Observable
- filter 過濾,把不符合條件的過濾掉,留下符合條件的
- take 指定最多輸出的數(shù)量
- doOnNext 允許我們在每次輸出一個元素之前做一些額外的事情
示例一
- create() 創(chuàng)建
- subscribe() 訂閱
- just()
- from() 盡管與just一樣是創(chuàng)建操作符,但是from操作符稍微強(qiáng)大點(diǎn)其垄。因?yàn)閒rom操作符的作用是將傳入的數(shù)組或 Iterable 拆分成具體對象后苛蒲,依次發(fā)送出來。
// TODO: 2016/12/16 創(chuàng)建被觀察者 observerable
Observable observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
subscriber.onNext("111");
subscriber.onNext("222");
subscriber.onNext("333");
subscriber.onNext("444");
subscriber.onCompleted();
}});
//// TODO: 2016/12/16 等價(jià)于just() from()
Observable.just("111", "222", "333");
Observable.from(new String[]{"111", "222", "333"});
// TODO: 2016/12/16 示例一 打印字符串?dāng)?shù)組
Observable.from(new String[]{"1", "2", "3"}).subscribe(new Action1<String>() { @Override
public void call(String s) {
DebugLog.i(s + "=====");
}});
class Course {
String name = "";
public Course(String name) { this.name = name; }
public String getCourse() { return " china " + name; }
}
class Student {
String name = "";
Course course[];
public Student(String name) {
this.name = name;
course = new Course[]{new Course(getName()), new Course(getName()), new Course(getName()), new Course(getName())};
}
public String getName() { return name; }
}
Student[] students = new Student[]{new Student("zhangsan"), new Student("lisi"), new Student("wangwu"), new Student("maliu")};
// TODO: 2016/12/16 flatmap 變換
Observer<Course> observer = new Observer<Course>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Course course) {
DebugLog.i(course.name + "====");
}
};
Observable.from(students).flatMap(new Func1<Student, Observable<Course>>() {
@Override
public Observable<Course> call(Student student) {
return Observable.from(student.course); }})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
// TODO: 2016/12/16 lift實(shí)現(xiàn) integer對象轉(zhuǎn)換 string
observable.lift(new Observable.Operator<String, Integer>() {
@Override
public Subscriber<? super Integer> call(final Subscriber<? super String> subscriber) {
return new Subscriber<Integer>() {
@Override
public void onCompleted() {
subscriber.onCompleted();
}
@Override
public void onError(Throwable e) {
subscriber.onError(e);
}
@Override
public void onNext(Integer integer) {
subscriber.onNext(integer + "=============");
}
};
}
});
示例二
map()特性:
- 它不必返回Observable對象返回的類型绿满,你可以使用map操作符返回一個發(fā)出新的數(shù)據(jù)類型的observable對象臂外。
- 可以對一個Observable多次使用map
Observable.just("Hellp Map Operator")
.map(new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return 2015;//通過第一個map轉(zhuǎn)成Integer
}
}).map(new Func1<Integer, String>() {
@Override
public String call(Integer integer) {
return String.valueOf(integer);//再通過第二個map轉(zhuǎn)成String
}
}).subscribe(new Action1<String>() {
@Override
public void call(String s) {
System.out.println(s);
}
});
示例三
Map是一對一的轉(zhuǎn)換,那么有沒有一對多的轉(zhuǎn)換呢喇颁?當(dāng)然有漏健,就是現(xiàn)在要說的 FlatMap
FlatMap操作符使用一個指定的函數(shù)對原始Observable發(fā)射的每一項(xiàng)數(shù)據(jù)執(zhí)行變換操作,這個函數(shù)返回一個本身也發(fā)射數(shù)據(jù)的Observable橘霎,然后FlatMap合并這些Observables發(fā)射的數(shù)據(jù)蔫浆,最后將合并后的結(jié)果當(dāng)做它自己的數(shù)據(jù)序列發(fā)射。
private final String url1 = "http://www.iamxiarui.com/wp-content/uploads/2016/06/套路.png";
private final String url2 = "http://www.iamxiarui.com/wp-content/uploads/2016/06/為什么我的流量又沒了.png";
private final String url3 = "http://www.iamxiarui.com/wp-content/uploads/2016/05/cropped-iamxiarui.com_2016-05-05_14-42-31.jpg";
private final String url4 = "http://www.iamxiarui.com/wp-content/uploads/2016/05/微信.png";
//一組Url數(shù)據(jù)private final String[] urls = new String[]{url1, url2, url3, url4};
然后來看flatmap如何處理:
//先傳遞String類型的UrlObservable.from(urls) .flatMap(new Func1<String, Observable<String>>() {
@Override
public Observable<String> call(String s) {
return Observable.just(s); } })
.map(new Func1<String, Bitmap>() {
@Override
public Bitmap call(String s) {
//通過Map轉(zhuǎn)換成Bitmap類型發(fā)送出去
return GetBitmapForURL.getBitmap(s);
} })
.subscribeOn(Schedulers.io()) // 指定subscribe()發(fā)生在IO線程 .observeOn(AndroidSchedulers.mainThread()) // 指定Subscriber的回調(diào)發(fā)生在UI線程 //可以看到姐叁,這里接受的類型是Bitmap瓦盛,而不是String .subscribe(new Action1<Bitmap>() {
@Override
public void call(Bitmap bitmap) { mainImageView.setImageBitmap(bitmap); mainProgressBar.setVisibility(View.GONE);
}
});