您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Android开发者们一定会喜欢的-RxJava
RxJavaBy侦跃&@hi头hiRxJava•扩展的观察者模式•处观察者模式•Observable发出事件•Subscriber订阅事件bus.post(newAnswerEvent(42));!@SubscribepublicvoidonAnswer(AnswerEventevent){!}Observableobservable=Observable.create(newObservable.OnSubscribeString(){ @Override publicvoidcall(Subscriber?superStringsubscriber){ subscriber.onNext(a); subscriber.onCompleted(); } }); observable.subscribe(newSubscriber(){… });观察者模式的不⾜足•时结•错误RxJava进•onCompleteSubscriber结•onErrorSubscriber错Observable.just(hello).subscribe(newSubscriberString(){@OverridepublicvoidonCompleted(){}!@OverridepublicvoidonError(Throwablee){}!@OverridepublicvoidonNext(Strings){System.out.println(s);}});unsubscribeSubscriptionsubscription=observable.subscribe(System.out::println); subscription.unsubscribe();处•观产线•订阅线•处Observable.just(hello) //观线产 .subscribeOn(Schedulers.newThread()) //观UI线 .observeOn(AndroidSchedulers.mainThread()) //为UI线程接收数据,//这UI .subscribe(System.out::println);Scheduler认RxJava产订阅线运!Scheduler换产订阅执线!RxJava认Scheduler针对务选择Schedulers•Schedulers.immediate()•Schedulers.trampoline()•Schedulers.newThread()•Schedulers.computation()•Schedulers.io()•AndroidSchedulers.mainThread()处-Operator•对进变换处•operators•义operatorObservable.just(1,2,3) .map(newFunc1Integer,String(){ @Override publicStringcall(Integerinteger){ returninteger.toString(); } }) .subscribe(newAction1String(){ @Override publicvoidcall(Strings){ System.out.println(s); } });Observable.just(1,2,3) .map(integer-integer.toString()) .subscribe(System.out::println);•组转换•务组•过滤掉⼩小于1•••Observable.just(1,2,2,3,4,5).map(Integer::parseInt).filter(s-s1).distinct().take(3).reduce((integer,integer2)-integer.intValue()+integer2.intValue()).subscribe(System.out::println);//9operators•创建Observablecreatejust•变换ObservablemapflatMap•过滤Observablefilterfirstlast•Observablemergezip•错误处理catch•过滤allskipUtiltakeWhile•averagereducecount义operatorObservable.just(“a”) .lift(subscriber-{ returnnewSubscriberString(){ @Override publicvoidonCompleted(){ subscriber.onCompleted(); } @Override publicvoidonError(Throwablee){ subscriber.onError(e); } @Override publicvoidonNext(Strings){ subscriber.onNext(1); } }; }) .map(i-i) .subscribe(System.out::println);//1Android应•Retrofit•RxAndroid•RxBinding•rx-preferences•sqlbrite•RxLifecycleRetrofit@GET(/story/{id})ObservableNewsDetailgetNewsDetailObservable(@Path(id)longid);RxAndroidAndroidSchedulers.mainThread()RxBindingRxJava对AndroidUI进RxView.clicks(findViewById(R.id.btn_throttle)) .throttleFirst(1,TimeUnit.SECONDS) .subscribe(aVoid-{ System.out.println(click); });RxPreferenceRxJavaSharePreferenceSharedPreferencespreferences=PreferenceManager.getDefaultSharedPreferences(context);!RxSharedPreferencesrxPreferences=RxSharedPreferences.create(preferences);!PreferenceStringusername=rxPreferences.getString(“username);!username.asObservable().subscribe(newAction1String(){@Overridepublicvoidcall(Stringusername){Log.d(TAG,Username:+username);}}RxBinding结合RxPreferenceSharedPreferencespreferences=PreferenceManager.getDefaultSharedPreferences(this); RxSharedPreferencesrxPreferences=RxSharedPreferences.create(preferences); PreferenceBooleanchecked=rxPreferences.getBoolean(checked,true); CheckBoxcheckBox=(CheckBox)findViewById(R.id.cb_test); RxCompoundButton.checkedChanges(checkBox) .subscribe(checked.asAction()); checked.asObservable().subscribe(aBoolean-{ System.out.println(----------------checked:+aBoolean); });RxLifecycleActivityFragment结myObservable.compose(RxLifecycle.bindUntilActivityEvent(lifecycle,ActivityEvent.DESTROY)).subscribe();myObservable.compose(RxLifecycle.bindActivity(lifecycle)).subscribe();RxJava场•现层调(Callbackhell)•杂处•应UI•杂线换层调getToken(newCallbackString(){@Overridepublicvoidsuccess(Stringtoken){getUser(userId,newCallbackUser(){@Overridepublicvoidsuccess(Useruser){userView.setUser(user);}!@Overridepublicvoidfailure(RetrofitErrorerror){//Errorhandling}};}!@Overridepublicvoidfailure(RetrofitErrorerror){//Errorhandling}});getToken(username,password).flatMap(token-getUser(token)).subscribe(user-{System.out.println(user:+user.toJson());});杂处Observable.just(1,2,2,3,4,5) .map(Integer::parseInt) .filter(s-s1) .distinct() .take(3) .reduce((integer,integer2)-integer.intValue()+integer2.intValue()).subscribe(System.out::println);//9应UICheckBoxcheckBox=(CheckBox)findViewById(R.id.cb_test); RxCompoundButton.checkedChanges(checkBox) .subscribe(checked.asAction()); 杂线换ObservableStringobservable1=createObservable1().subscribeOn(Schedulers.newThread()); ObservableStringobservable2=createObservable2().subscribeOn(Schedulers.io()); Observable.concat(observable1,observable2) .subscribeOn(Schedulers.computation()) .subscribe(System.out::println);RxJava•库体积稍⼤大(900K••对时unsubscribeRxBinding库)•Java6lambda简化代码(Retrolambda&Gradle••扔物线的:给Android发RxJava详解•RxJava应⽤用案例•Square•SoundCloud•Flipboard•动•ThanksQ&A
本文标题:Android开发者们一定会喜欢的-RxJava
链接地址:https://www.777doc.com/doc-4239424 .html