Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView not calling onCreateViewHolder or onBindView

Not getting any errors and all the data seems valid. For some reason, nether of the view related methods are being called. I have made sure of the following:

  • getItemCount() is the only adapter method being called and is returning a positive integer value, (I know this will be the area you guys will look at)

    • Constructor is being called, member variables are valid.

    • Parent View is a vertical LinearLayout; no scrollview, or any other view with their own scroll properties in sight.

    • containing fragment view is created and shown on screen.

Here is the declaration in the fragment followed by the adapter. Any help would be appreciated as this has be completely baffled.

SubMenuAdapter adapter = new SubMenuAdapter(getActivity(), mContentItems);     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));     recyclerView.setItemAnimator(new DefaultItemAnimator());     recyclerView.setAdapter(adapter); 
public class SubMenuAdapter extends RecyclerView.Adapter<SubMenuAdapter.ViewHolder> { private static final String TAG = String.format("==> %S", SubMenuAdapter.class.getSimpleName());  private final List<ContentItem> mContentItems; private Context mContext;  public SubMenuAdapter(Context context, List<ContentItem> contenItems) {     Log.d(TAG, "Constructor called");     mContentItems = contenItems;     mContext = context; }  @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {     Log.d(TAG, "onCreateViewHolder called");     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_resource_efficiency, parent, false);     return new ViewHolder(view); }  @Override public void onBindViewHolder(ViewHolder holder, int position) {     Log.d(TAG, "onBindViewHolder called");      ContentItem item = mContentItems.get(position);     holder.textName.setText(item.getName());     FontSetter.setMyriadProRegular(mContext, holder.textName);     Picasso.with(mContext).load("file://" + item.getPreviewImageDefault()).into(holder.imageIcon); }  @Override public int getItemCount() {     Log.d(mContext, String.format("getItemCount: %d", mContentItems.size()));     return mContentItems.size(); }  // ViewHolder public static class ViewHolder extends RecyclerView.ViewHolder {      TextView textName;     ImageView imageIcon;      public ViewHolder(View view) {         super(view);         textName = (TextView) view.findViewById(R.id.tv_resource_efficiency_option);         imageIcon = (ImageView) view.findViewById(R.id.iv_resource_efficiency_icon);     } } 
like image 936
Conti Avatar asked Mar 11 '15 17:03

Conti


People also ask

How many times is called onCreateViewHolder?

By default it have 5. you can increase as per your need.

How do I fix no adapter attached skipping layout?

setLayoutManager(new LinearLayoutManager(this)); recyclerView. setAdapter(adapter); Just replace above code with this and it should work. What you did wrong is you called setAdapter(adapter) before calling layout manager.

What is the difference between onCreateViewHolder and onBindViewHolder?

When used, pass the child Class Object, and in onCreateViewHolder , use Reflection to create the child ViewHolder. When you get an onBindViewHolder, just pass it to the ViewHolder.


2 Answers

This may also help someone

First Use

recyclerView.setAdapter(adapter); 

And then:

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

So it will look like this:

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

The order is reversed

Update:

Nowadays I simply use:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" 

on the RecyclerView in the xml

like image 188
Aerim Avatar answered Sep 29 '22 06:09

Aerim


Been chasing answer for over an hour.

Dont forget to call this one liner before setting your adapter.

recyclerView.setLayoutManager(new LinearLayoutManager(this)); 

It seems that recycler view do not have default layout manager option built in so we have to programatically add it.

Cheers if you found it helpful.

like image 25
ralphgabb Avatar answered Sep 29 '22 08:09

ralphgabb