如何使用Rexview与Rxjava和Retrofit

时间:2017-05-15 22:42:48

标签: java android retrofit2 rx-java2

我尝试使用谷歌搜索来获取从网络调用到Recyclerview的值的某种方式。但我无法弄清楚如何使用Rxjava将值设置为适配器

private void initViews()
{
    recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerview.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerview.setLayoutManager(layoutManager);
    loadRxjava();

}

private void loadRxjava()
{
    Subscription sub = retrofitManager.getModelNew().postRawJson()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Subscriber<ModelNew>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(ModelNew modelNew)
                {

                }
            });
}

1 个答案:

答案 0 :(得分:0)

这可能是首发

   Observable.just(findViewById(R.id.recycler_view))
            .map( o -> (RecyclerView) o)
            .map(recyclerview -> 
                    {recyclerview.setHasFixedSize(true);
                     return recyclerview;
                    })
            .flatMap(recyclerview -> Observable.just(new LinearLayoutManager(getApplicationContext()))
                    .map(layout -> {
                        recyclerview.setLayoutManager(layout)
                                return recyclerview;

                    }))
            .subscribe(recyclerview -> { retrofitManager.getModelNew().postRawJson();
      //TODO:Here you can mix recyclerview with modelView})

我不知道你的代码是做什么的,所以不确定这是不是你想要的。

如果你想看看RXJava的实例,请看这里https://github.com/politrons/reactive