package com.example.five_rxjava;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.observables.GroupedObservable;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//被观察者
// mapOpretor();
// flatMapOpretor();
// groupByOperator();
}
//过滤操作符filter
// public void filterOperator(){
// Observable.just(Observable.range(1,100)).filter(new Predicate<Integer>() {
// @Override
// public boolean test(Integer integer) throws Exception {
// return false;
// }
// })
// }
//distinc过滤掉重复的数据项用来去重
//ignoreElements不发射任何数据
//elementAt只发射第n项数据
//分组操作符groupby
public void groupByOperator(){
Observable
.range(1,8)
.groupBy(new Function<Integer, String>() {
@Override
public String apply(Integer integer) throws Exception {
return (integer %2 == 0)?"偶数组":"奇数组";
}
}).subscribe(new Consumer<GroupedObservable<String, Integer>>() {
@Override
public void accept(GroupedObservable<String, Integer> stringIntegerGroupedObservable) throws Exception {
Log.d("jioushu",stringIntegerGroupedObservable.getKey());
}
});
}
public void opretor() {
new Observable<String>() {
@Override
protected void subscribeActual(Observer<? super String> observer) {
observer.onNext("玻璃瓶");
observer.onNext("啤酒瓶");
observer.onComplete();
}
}.subscribeActual(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.d("pingzi", s);
}
@Override
public void one rror(Throwable e) {
}
@Override
public void onComplete() {
Log.d("pingzi", "执行完毕");
}
});
}
//map
public void mapOpretor() {
Observable.just("HELLO").map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s.toLowerCase();
}
}).map(new Function<String, String>() {
@Override
public String apply(String s) throws Exception {
return s + "world";
}
}).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.d("nihao", s);
}
@Override
public void one rror(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
//flatmap
public void flatMapOpretor() {
User user = new User();
user.addresses = new ArrayList<>();
user.username = "zhangsan";
User.Address address = new User.Address();
address.city = "北京";
address.street = "上地九街";
user.addresses.add(address);
User.Address address2 = new User.Address();
address2.city = "上海";
address2.street = "上地十街";
user.addresses.add(address2);
Observable.just(user)
.flatMap(new Function<User, ObservableSource<User.Address>>() {
@Override
public ObservableSource<User.Address> apply(User user) throws Exception {
return Observable.fromIterable(user.addresses);
}
}).subscribe(new Consumer<User.Address>() {
@Override
public void accept(User.Address address) throws Exception {
Log.d("dizhi", address.city + address.street);
}
});
//subscribe
// Observable.just(user)
// .map(new Function<User, List<User.Address>>() {
// @Override
// public List<User.Address> apply(User user) throws Exception {
// return user.addresses;
// }
// }).subscribe(new Consumer<List<User.Address>>() {
// @Override
// public void accept(List<User.Address> addresses) throws Exception {
// for (int i = 0; i < addresses.size(); i++) {
// Log.d("dizhi",addresses.get(i).city+addresses.get(i).street);
// }
// }
// });
}
}