Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recyclerview not call onCreateViewHolder

Another one is make sure you set layout manager to RecyclerView:

recycler.setLayoutManager(new LinearLayoutManager(this));

Your getItemCount method returns 0. So RecyclerView never tries to instantiate a view. Make it return something greater than 0.

for example

@Override
public int getItemCount() {
    return data.size();
}

i forgot to add below line after i adding it worked for me

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

I really really hope this helps someone someday. I just spent over an hour on this one going nuts!

I had this:

projects_recycler_view.apply {
            this.layoutManager = linearLayoutManager
            this.adapter = adapter
        }

When I should have had this:

projects_recycler_view.apply {
            this.layoutManager = linearLayoutManager
            this.adapter = projectAdapter
        }

Because I foolishly called my adapter adapter, it was being shadowed by the recycler view's adapter in the apply block! I renamed the adapter to projectAdapter to differentiate it and problem solved!


Please set layout manager to RecyclerView like below code:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_right);
//set your recycler view adapter here
NavigationAdapter adapter = new NavigationAdapter(this, FragmentDrawer.getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

This does not apply for your particular case. But this might help someone else.

This reason could be careless usage of the following method

recyclerView.setHasFixedSize(true);

If not used with caution, this might cause the onBindView and onCreateViewHolder to not be called at all, with no error in the logs.


Maybe it helps someone but if you set your RecycleView's visibility to GONE the adapter methods won't be called at all... Took me some time to figure it out.