本文主要是介绍RxJava+RxAndroid 初学者一看一敲就懂,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
惯例:先上图后说话,谢谢各位伙伴的支持! 有你们是我的福分!
每日一言:自信的生命最美丽。
使用
1、关联
compile 'io.reactivex:rxjava:1.0.14' compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.trello:rxlifecycle:0.3.1' compile 'com.trello:rxlifecycle-components:0.3.1'
2、简单示例(一定自己敲一遍)
String tag = "=====>:"; public void test01() {Log.e(tag, "------------test01-----------" +"\n----------简单实例01---------"); //创建被观察者 Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {@Override public void call(Subscriber<? super String> subscriber) {//调用观察者的回调 subscriber.onNext("我是"); subscriber.onNext("RxJava"); subscriber.onNext("简单示例"); subscriber.onError(new Throwable("出错了")); subscriber.onCompleted(); }}); //创建观察者 Observer<String> observer = new Observer<String>() {@Override public void onCompleted() {Log.e(tag, "onCompleted"); }@Override public void onError(Throwable e) {Log.e(tag, e.getMessage()); }@Override public void onNext(String s) {Log.e(tag, s); }}; //注册,是的观察者和被观察者关联,将会触发OnSubscribe.call方法 observable.subscribe(observer); }public void test02() {//创建被观察者 final Observable<String> observable=Observable.create(new Observable.OnSubscribe<String>(){@Override public void call(Subscriber<? super String> subscriber) {//调用观察者的回调 subscriber.onNext("我是"); subscriber.onNext("RxJava"); subscriber.onNext("简单实例02"); subscriber.onCompleted(); subscriber.onError(new Throwable("出错了")); }}); //创建观察者 Subscriber<String> subscriber=new Subscriber<String>() {@Override public void onStart() {Log.e(tag,"onStart"); }@Override public void onCompleted() {Log.e(tag,"onCompleted"); }@Override public void onError(Throwable e) {Log.e(tag,e.getMessage()); }@Override public void onNext(String s) {Log.e(tag,s); }}; //注册,是的观察者和被观察者关联,将会触发OnSubscribe.call方法 observable.subscribe(subscriber); } public void test03(){String[] ints={"1","2","3","4"}; Observable.from(ints).subscribe(new Subscriber<String>() {@Override public void onCompleted() {Log.e(tag,"onCompleted"); }@Override public void onError(Throwable e) {Log.e(tag,e.getMessage()); }@Override public void onNext(String s) {Log.e(tag,s); }}); } public void test04(){Observable.just(1,2,3,4,2).subscribe(new Subscriber<Integer>() {@Override public void onCompleted() {Log.e(tag,"onCompleted"); }@Override public void onError(Throwable e) {Log.e(tag,e.getMessage()); }@Override public void onNext(Integer integer) {Log.e(tag,integer+""); }}); } public void test06(){Log.e(tag, "------------test06-----------" +"\n----------线程调度---------"); //从网络上根据用户id,请求对应用户,并显示用户积分到界面 Observable.create(new Observable.OnSubscribe<Integer>() {@Override public void call(Subscriber<? super Integer> subscriber) {int id=111; UserInfo userinfo=getUserInfoFromNet(id); subscriber.onNext(userinfo.points); }}).subscribeOn(Schedulers.io())//事件产生在io线程 .observeOn(AndroidSchedulers.mainThread())//消耗事件在主线程 .subscribe(new Action1<Integer>() {@Override public void call(Integer points) {Log.e(tag, "显示用户积分:" + points); }}); } //模拟从网络请求 private UserInfo getUserInfoFromNet(int id) {UserInfo userInfo = new UserInfo(); userInfo.points=100; return userInfo; } public class UserInfo{public int id; public int points; } public void test10(){Log.e(tag, "------------test10-----------" +"\n----------操作符---------"); String[] strs={"aa","bb","bb","cc","dd","ee"}; Observable<String> observable = Observable.from(strs); //filter(Func1)方法来过滤我们观测序列中不想要的值 //take(count)方法来限制获取多少个数据 Log.e(tag,"---------filter & take----------"); observable.filter(new Func1<String, Boolean>() {@Override public Boolean call(String s) {//把已b结尾的数据去掉 return !s.endsWith("b"); }}).take(3)//取3个数据 .subscribe(new Action1<String>() {@Override public void call(String s) {Log.e(tag,s); }}); Log.e(tag,"---------skip & first----------"); observable.skip(3).first().subscribe(new Action1<String>() {@Override public void call(String s) {Log.e(tag,s); }}); }
感谢果风兄弟的支持和指导
这篇关于RxJava+RxAndroid 初学者一看一敲就懂的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!