您现在的位置是:首页 > 文章详情

RxJava2 / RxAndroid2的过滤器filter

日期:2017-05-14点击:219
RxJava2 / RxAndroid2的过滤器filter

filter完成一个条件过滤和筛选,从Observable的输出数据中,经过filter筛选一层,如果filter判断的值为真,则交给观察者,否则跳过,例如代码:
package zhangphil.app; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.functions.Predicate; import io.reactivex.observers.DisposableObserver; import io.reactivex.schedulers.Schedulers; public class MainActivity extends AppCompatActivity { private final String TAG = getClass().getSimpleName(); private CompositeDisposable mCompositeDisposable = new CompositeDisposable(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); test(); } private void test() { DisposableObserver disposableObserver = new DisposableObserver<Integer>() { @Override public void onNext(Integer i) { Log.d(TAG, "#####开始#####"); Log.d(TAG + "数据", String.valueOf(i)); Log.d(TAG, "#####结束#####"); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } @Override public void onError(Throwable e) { Log.e(TAG, e.toString(), e); } }; mCompositeDisposable.add( getObservable().filter(new Predicate<Integer>() { @Override public boolean test(Integer integer) throws Exception { int result = integer % 10; boolean b = (result == 0); return b; // 此处返回的条件若为true,则数据交给观察者 ,否则跳过 } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(disposableObserver)); } private Observable<Integer> getObservable() { return Observable.range(0, 100); } @Override protected void onDestroy() { super.onDestroy(); // 如果退出程序,就清除后台任务 mCompositeDisposable.clear(); } } 


代码输出:

05-15 17:15:49.664 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.664 29872-29872/zhangphil.app D/MainActivity数据: 0 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 10 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 20 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 30 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 40 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 50 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 60 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 70 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 80 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####开始##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity数据: 90 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: #####结束##### 05-15 17:15:49.665 29872-29872/zhangphil.app D/MainActivity: onComplete


原文链接:https://yq.aliyun.com/articles/615870
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章