久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

android - RxJava中如何根據(jù)已有的函數(shù)或函數(shù)回調(diào)創(chuàng)建Observable?

瀏覽:125日期:2024-02-23 08:38:13

問題描述

在使用Rxjava過程中,可能已經(jīng)有很多函數(shù)回調(diào),那么怎么根據(jù)這些函數(shù)回調(diào)的參數(shù)創(chuàng)建數(shù)據(jù)流?比如如果我需要改造onKeyDown(),那么怎么根據(jù)傳來按鍵的不同,處理特定用戶輸入的序列,比如用戶輸入“1,2,3,4”的時候做特殊處理。

或者有其他的函數(shù)回調(diào),怎么將這些函數(shù)回調(diào)的數(shù)據(jù)使用bufferDebouncezip等操作符處理數(shù)據(jù)?

問題解答

回答1:

可以這樣寫

private BehaviorSubject<Integer> bs; private void testSeri() {bs = BehaviorSubject.create();//每3次 accept 一次bs.buffer(3).subscribe(new Consumer<List<Integer>>() { @Override public void accept(@NonNull List<Integer> ints) throws Exception {StringBuilder sb = new StringBuilder();for (int i = 0; i < ints.size(); i++){ sb.append(ints.get(0));}Toast.makeText(TestSubjectActivity.this, sb.toString(), Toast.LENGTH_SHORT).show(); }}); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {bs.onNext(keyCode);return super.onKeyDown(keyCode, event); }

onKeyDown是Activity的回調(diào),不方便再包裝一層,因此用了Subject這種可以【隨時隨地】發(fā)射數(shù)據(jù)、訂閱和發(fā)射方便分開寫的發(fā)射器。對于一般的回調(diào)可以這樣寫,給你個百度定位的回調(diào)感受一下

class LocationObservable implements ObservableOnSubscribe<BDLocation> {@Overridepublic void subscribe(final ObservableEmitter<BDLocation> e) throws Exception { initLocation(); mLocationClient.registerLocationListener( new BDLocationListener(){@Overridepublic void onReceiveLocation(BDLocation location) { if (location != null) {mLocationClient.stop();if (!TextUtils.isEmpty(location.getCity())) { e.onNext(location); e.onComplete();} } else {// 定位失敗e.onError(new Exception('百度地圖 定位失敗')); }} } ); mLocationClient.start();} }

對于一般的函數(shù),可以這樣

Observable<String> o1 = Observable.fromCallable(new Callable<String>() { @Override public String call() {return func1(); }});public String func1(){ return 'ok';}

標簽: java
相關(guān)文章:
主站蜘蛛池模板: 石渠县| 梅州市| 镇江市| 永胜县| 丁青县| 宕昌县| 健康| 慈溪市| 永吉县| 二连浩特市| 尼玛县| 涪陵区| 南华县| 揭阳市| 乐平市| 丰县| 镇宁| 江陵县| 兴城市| 山丹县| 弋阳县| 普兰店市| 乐业县| 库伦旗| 康马县| 宿州市| 丹棱县| 新乐市| 延津县| 贵阳市| 宜黄县| 平湖市| 贵州省| 鹤峰县| 潜山县| 济南市| 明光市| 瑞金市| 若羌县| 德昌县| 丹江口市|