Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use inside a fragment? #9

Open
jaambee opened this issue Nov 23, 2016 · 3 comments
Open

Use inside a fragment? #9

jaambee opened this issue Nov 23, 2016 · 3 comments

Comments

@jaambee
Copy link

jaambee commented Nov 23, 2016

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?

@ahmedrizwan
Copy link
Owner

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...

@CodeK1988
Copy link

haha!I have not encountered any problems

@jaambee
Copy link
Author

jaambee commented Nov 30, 2016

I make the corrections, but i get the same result. My fragment is inside of a ViewPager. Can be this the problem ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants