文章来自:http://blog.csdn.net/intbird
lib_github:https://github.com/ReactiveX/RxAndroid
github:https://github.com/intbird/ANDIntbirdRxjava
1,grade config
compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.14'
2
1->sample just
private void just(){
Observable<String> myObser = Observable.just("hello ","intbird ");
Action1<String> actionCallNext = new Action1<String>() {
@Override
public void call(String s) {
Log.d(TAG,s);
}
};
Action1<Throwable> actionOnError = new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.d(TAG,throwable.toString());
}
};
Action0 actionComplet = new Action0(){
@Override
public void call() {
}
};
myObser.subscribe(actionCallNext,actionOnError,actionComplet);
}
private void justMapStr(){
Observable<String> myObser = Observable.just("hello ");
Action1 action1 = new Action1<String>() {
@Override
public void call(String s) {
Log.d(TAG,s);
}
};
Func1<String,Object> func1 = new Func1<String, Object>() {
@Override
public Object call(String s) {
return s+"intbird";
}
};
myObser.map(func1).subscribe(action1);
}
private void justMapInt(){
Observable<String> myObser = Observable.just("hello ");
Action1 action1 = new Action1<Integer>() {
@Override
public void call(Integer s) {
Log.d(TAG,s+"");
}
};
Func1<String,Integer> func1 = new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return s.hashCode();
}
};
myObser.map(func1).subscribe(action1);
}
private void justSubscriber() {
Observable<String> myObservable = Observable.just("hello");
Subscriber<String> mySubscriber = new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
Log.d(TAG,s);
}
};
myObservable.subscribe(mySubscriber);
}
3,list flatmap
Func1 func1 = new Func1<List<String>, Observable<String>>() {
@Override
public Observable<String> call(List<String> strings) {
return Observable.from(strings);
}
};
Func1 func11 = new Func1<String, Observable<?>>() {
@Override
public Observable<?> call(String s) {
return Observable.just(s);
}
};
Func1 func111 = new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return s != null;
}
};
Action1<String> action1 = new Action1<String>() {
@Override
public void call(String s) {
Log.d(TAG,"flatMap "+ s);
}
};
query("hello","_","intbird",null,null,"notnull")
.flatMap(func1)
.flatMap(func11)
.filter(func111)
.take(5)
.doOnNext(new Action1() {
@Override
public void call(Object o) {
Log.d(TAG,"save data = " +o.toString());
}
})
.subscribe(action1);
2->
public class RxOperatorActivity extends AppCompatActivity {
private String TAG = "RxActivityTag";
TextView tvHello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rx);
tvHello = (TextView)findViewById(R.id.tv_hello);
//TODO///////基本操作;
//just();//直接简单创建Observer;
defalutSubscriber(Observable.just(1l));
//create();//自定义创建Observer;
defalutSubscriber(Observable.create(subscriber -> {
subscriber.onNext(1l);
subscriber.onCompleted();
}));
//range();//Integer
//defalutSubscriber(Observable.range(10, 5));
//defer();//到next时才去获取表达式的值,timeMills为next中计算的值;
defalutSubscriber(Observable.defer(() -> Observable.just(System.currentTimeMillis())));
//from();//列表
Long[] integerArray = new Long[]{1l,2l,3l,4l,5l};
defalutSubscriber(Observable.from(integerArray));
//interval();//computation scheduler,3秒一次,持续发送
defalutSubscriber(Observable.interval(3, TimeUnit.SECONDS));
//timer();//3秒后发送,延迟发送一次;
defalutSubscriber(Observable.timer(3, TimeUnit.SECONDS));
//repeat();//重复n次发送当前结果;
defalutSubscriber(Observable.just(1l).repeat(3));
//TODO/数据分组和修改;
//buffer();//收到n个数据后才开始发送[普通对象和值],中途可以跳过m个数据
//隔2个数字发出前一个数字
Observable<List<Integer>> buffer = Observable.just(1, 2, 3).buffer(1, 2);
//每隔3秒钟输出2~4个数字
Observable.interval(1, TimeUnit.SECONDS).buffer(3, TimeUnit.SECONDS);
//window();//收到n个数据后才开始发送[Observer对象];
//隔2个数字发出前一个数字
Observable<Observable<Integer>> window = Observable.just(1, 2, 3).window(1, 2);
//每隔3秒钟输出2~4个Observer对象;
Observable.interval(1,TimeUnit.SECONDS).window(3, TimeUnit.SECONDS);
//groupBy();分组 1,2,3,4,5,6,7,8,9 -> (1,3,5.7)(2,4,6,8)
//Observable.just(1,2,3,4,5).groupBy();
//flatMap();
// 将之前的observer进行转换成我们需要的类型的observer;
defalutSubscriber(Observable.just(1, 2, 3, 4, 5)
.flatMap(integer -> Observable.just((long) integer)));
//flatMapIterable();
//将之前的observer进行转换成Iterator;
Observable.just(1,2,3,4,5)
.flatMapIterable(integer -> {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < integer; i++) {
list.add(i);
}
return list;
});
//map(); //数据修改,任意转换;
defalutSubscriber(Observable.just(1, 2, 3, 4, 5).map(integer -> (long) integer));
//cast();//数据修改,类型转换,将Integer转换为Long类型;
defalutSubscriber(Observable.just(1).cast(Long.class));
//scan();//应用一个表达式,返回表达式之后的值; ((1+2)+3)+...
defalutSubscriber(Observable.just(1l, 2l, 3l).scan((aLong1, aLong2) -> aLong1 + aLong2));
//TODO应用规则舍或弃保留数据
//throttleWithTimeout();//两次数据发送间隔小于200毫秒的数据将被丢弃;
Observable.just(1,2,3,4,5).throttleWithTimeout(200, TimeUnit.MILLISECONDS);
//dedounce();//两次发送数据的间隔小于200毫秒的数据将被丢弃;
Observable.just(1,2,3,4,5).debounce(200, TimeUnit.MILLISECONDS);
//dedounce();//如果返回当前数据时,上次的计算还在进行,那么丢弃上次计算,计算该次数据,依次do this;
Observable.just(1,2,3,4,5).debounce(integer -> Observable.create(subscriber -> {
subscriber.onNext(integer);
subscriber.onCompleted();
}));
//distinctObserver();//去除 所有重复 数据;
defalutSubscriber(Observable.just(1l,2l,3l,4l,5l,4l,3l,2l,1l).distinct());
//distinctUntilChanged;//去除 连续重复 数据;
defalutSubscriber(Observable.just(1l, 2l, 3l, 4l, 5l, 4l, 4l, 2l, 2l).distinctUntilChanged());
//elementAt();//获取指定位置的数据;
defalutSubscriber(Observable.just(1l,2l,3l).elementAt(2));
//take();//保留前两个数据,舍弃其他;
defalutSubscriber(Observable.just(1l,2l,3l).take(2));
//skip();//舍弃前两个数据,保留其他;
defalutSubscriber(Observable.just(1l, 2l, 3l).skip(2));
//filter();//根据条件过滤;
defalutSubscriber(Observable.just(1l, 2l, 3l).filter(i -> i > 1l));
//first();//符合条件的第一条数据;
defalutSubscriber(Observable.just(1l, 2l, 3l).first());
//BlockingObservable();//阻塞Observer,满足条件时才会发射;
BlockingObservable<Long> longBlockingObservable = Observable.just(1l,2l,3l).toBlocking();
longBlockingObservable.first();
//sample(); //类似 throttleLast()
Observable.just(1l,2l,3l).sample(1000, TimeUnit.SECONDS);
//throttleLast(); //1秒发射一次,保留第一次的结果; 如 1,2,3,4,5 (1,2)在1秒内完成,那么2将被丢弃;
Observable.just(1l,2l,3l).throttleLast(1000,TimeUnit.SECONDS);
//throttleFirst(); //1秒发射一次,保留距离最近的一次操作;如 1,2,3,4,5 (1,2)在1秒内完成,那么1将被丢弃;
Observable.just(1l,2l,3l).throttleFirst(1000, TimeUnit.SECONDS);
//combineLast();//可以将2-9个Observer发射的数据组装起来再发射出去;/ ob3 = combine(ob1,ob2,opt); ob3 = combine(oblist,opt);
defalutSubscriber(Observable.combineLatest(defaultObserver(1l), defaultObserver(2l), (integer, integer2) -> integer + integer2));
//Join();
Observable.just("Left-").join(defaultObserver(1l),
integer -> Observable.just(1),
integer -> Observable.timer(2000, TimeUnit.MILLISECONDS),
(i, j) -> i + j
);
//merege(); //合并两个observer的结果,如果有错误则终止发送;
defalutSubscriber(Observable.merge(defaultObserver(1l), defaultObserver(2l)));
//mergeDelayError();合并两个observer的结果,将过程中的错误放在next最后输出;
defalutSubscriber(Observable.mergeDelayError(defaultObserver(1l), defaultObserver(2l)));
//startWith();//输出顺序为 -1 0 1;
defalutSubscriber(defaultObserver(1l).startWith(-1l, 0l));
//switchOnNext();//结合两个Observer,如果第一个ob的最后一个数据还在计算中发出了第二个ob,那么第一个ob的最后一个数据将被丢弃;
defalutSubscriber(Observable.switchOnNext(Observable.create(subscriber -> subscriber.onNext(defaultObserver(1l)))));
//zip(); zipWith();
}
private Observable<Long> defaultObserver(long index){
return Observable.create(subscriber -> {
subscriber.onNext(index);
subscriber.onCompleted();
});
}
private void defalutSubscriber(Observable<Long> observable){
Observable<String> observableStr =
observable.map(Object::toString)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.computation());
observableStr.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String s) {
tvHello.setText(s);
}
});
}
3->more click go to github:https://github.com/intbird/ANDIntbirdRxjava