首先给出Github上的源码
RxJava:https://github.com/ReactiveX/RxJava
RxAndroid:https://github.com/ReactiveX/RxAndroid
添加依赖
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
一.基本使用
创建观察者Observer
第一种方式:
第二种方式:
创建被观察者Observable
第一种方式:
第二种方式:
第三种方式:
执行结果截图:
二.自定义调用
代码实现:
执行结果截图:
三.自定义调用举例
代码实现:
执行结果截图:
四.线程控制Sheduler
执行结果截图:
五.RxJava之转换-map()
代码实现:
执行结果截图:
把对象装换成String 类型输出:
代码实现:
Actor actor1 = new Actor("周星驰", "男");
Actor actor2 = new Actor("张柏芝", "女");
ArrayList<Actor> movie1List = new ArrayList<Actor>();
movie1List.add(actor1);
movie1List.add(actor2);
Movie movie1 = new Movie("1998-10-14", 1, movie1List, "喜剧之王");
Actor actor3 = new Actor("罗志祥", "男");
Actor actor4 = new Actor("张雨绮", "女");
ArrayList<Actor> movie2List = new ArrayList<Actor>();
movie2List.add(actor3);
movie2List.add(actor4);
Movie movie2 = new Movie("2016-05-01", 2, movie2List, "美人鱼");
Movie[] movies = {movie1, movie2};
//这是把对象装换成String 类型输出 -------map
//首先,定义观察者
Observer observer = new Observer<String>() {
@Override
public void onCompleted() {
Log.i(TAG, "Completed方法执行了");
}
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError方法执行了");
}
@Override
public void onNext(String s) {
Log.i(TAG, "onNext方法执行了");
Log.i(TAG,"s==="+s);
}
};
//定义被观察者
Observable observable= Observable.from(movies).map(new Func1<Movie, String>() {
@Override
public String call(Movie movie) {
return movie.toString();
}
});
observable.subscribe(observer);
Actor.java
public class Actor {
private String name;
private String sex;
public Actor(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Actor{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
Movie.java
public class Movie
{
private String name;
private int id;
private String data;
private List<Actor> mactorList;
public Movie(String data, int id, List<Actor> mactorList, String name)
{
this.data = data;
this.id = id;
this.mactorList = mactorList;
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Actor> getMactorList() {
return mactorList;
}
public void setMactorList(List<Actor> mactorList) {
this.mactorList = mactorList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Movie{" +
"data='" + data + '\'' +
", name='" + name + '\'' +
", id=" + id +
", mactorList=" + mactorList +
'}';
}
}
执行结果截图:
五.RxJava之转换-flatMap()
把一个电影装换成多个演员输出,并打印出演员的名字------flatMap
Actor actor1 = new Actor("周星驰", "男");
Actor actor2 = new Actor("张柏芝", "女");
ArrayList<Actor> movie1List = new ArrayList<Actor>();
movie1List.add(actor1);
movie1List.add(actor2);
Movie movie1 = new Movie("1998-10-14", 1, movie1List, "喜剧之王");
Actor actor3 = new Actor("罗志祥", "男");
Actor actor4 = new Actor("张雨绮", "女");
ArrayList<Actor> movie2List = new ArrayList<Actor>();
movie2List.add(actor3);
movie2List.add(actor4);
Movie movie2 = new Movie("2016-05-01", 2, movie2List, "美人鱼");
Movie[] movies = {movie1, movie2};
//把一个电影装换成多个演员输出,并打印出演员的名字------flatMap
Observer<Actor> observer = new Observer<Actor>() {
@Override
public void onCompleted() {
Log.i(TAG, "Completed方法执行了");
}
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError方法执行了");
}
@Override
public void onNext(Actor actor) {
Log.i(TAG, "actor===" + actor.getName());
}
};
Observable.from(movies).flatMap(new Func1<Movie, Observable<Actor>>() {
@Override
public Observable<Actor> call(Movie movie) {
return Observable.from(movie.getMactorList());
}
}).subscribe(observer);
执行结果截图:
总结:本文主要是介绍了RxJava的基本使用,没有涉及到底层原理部分,本文在写作过程中参考了@扔物线的博客,在此表示感谢!
有关更加详细的介绍,请参考:
代码亲测可用,代码下载地址: