RxRecyclerAdapter
RxRecyclerAdapter copied to clipboard
Use inside a fragment?
Hi, i have this code on my fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
fragmentHomeBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_home, container, false);
fragmentHomeBinding.rv.setLayoutManager(new LinearLayoutManager(getContext()));
fragmentHomeBinding.rv.setHasFixedSize(true);
//Dummy DataSet
mCompositeSubscription.add(HaipService.getMyPendingCampaigns()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.flatMap(campaigns -> {
pending = new RxDataSource<>(campaigns);
return pending.<MainItemBinding>bindRecyclerView(fragmentHomeBinding.rv, R.layout.main_item);
})
.doOnNext(campaignMainItemBindingSimpleViewHolder -> {
fragmentHomeBinding.rv.setAdapter(pending.getRxAdapter());
})
.subscribe(viewHolder -> {
MainItemBinding item = viewHolder.getViewDataBinding();
Campaign campaign = viewHolder.getItem();
Log.d("I'm here", campaign.getName());
Picasso.with(getContext()).load(campaign.getLogo()).fit().centerInside().into(item.brandImage);
Picasso.with(getContext()).load(campaign.getImage())
.fit().centerCrop().into(item.campaignImage);
item.brandName.setText(campaign.getName());
item.progressBar.setProgress(campaign.getCompleteStatus());
item.completedPercent.setText(String.valueOf(campaign.getCompleteStatus()) + "%");
}, Throwable::printStackTrace));
return fragmentHomeBinding.getRoot();
}
but when I run the app, i'm only get
E/RecyclerView: No adapter attached; skipping layout
If I test the same code on an Activity, it works (changing DataBindingUtil.inflate by DataBindingUtil.setContentView).
Is this the correct way to bind a recyclerView inside a fragment?
Hi! I'll look into it... You're using the adapter correctly... Just a few pointers for now, that you can check...
While inflating the Fragment through Databinding, you don't need to create a new Inflater... You do get an inflater object in onCreateView...
fragmentHomeBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
Secondly... You also don't need to call setAdapter explicitly on the RecyclerView... bindRecyclerView method takes care of that also... Hence your doOnNext can be omitted...
Anyway... I'll see if I can fix the No adapter attached issue, and will let you know soon...
haha!I have not encountered any problems
I make the corrections, but i get the same result. My fragment is inside of a ViewPager. Can be this the problem ?