RecyclerView not calling onCreateViewHolder or onB

2019-01-14 05:26发布

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);
    }
}

15条回答
在下西门庆
2楼-- · 2019-01-14 05:46

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

查看更多
你好瞎i
3楼-- · 2019-01-14 05:46

In my case it was using setHasStableIds(true); in the adapter while some list items were returning -1 in getItemID(), so I suppose the RecyclerView was thinking they are equal.

查看更多
够拽才男人
4楼-- · 2019-01-14 05:48

This is really old already, but in my case, I was using a ConstraintLayout and the height was set wrong so my view would have 0dp and then the adapter would be saving to do its job, as the view is 0dp.

Make sure height and width is ok and RecyclerView is has a positive size.

查看更多
爷的心禁止访问
5楼-- · 2019-01-14 05:49

My two pennies here. Just spent more than three hours debugging this.

Make sure that you have not used the following line carelessly.

recyclerView.setHasFixedSize(true);

Set the fixed size only when you are sure that the view would have a constant size. In case it is not, the onCreateViewHolder and onBindView methods might not get called at all.

查看更多
趁早两清
6楼-- · 2019-01-14 05:49

I know this topic is old, but if you did the same learner mistake, as I did, you might find this helpful.

As I was studying on some website about RecyclerView and copying some code and adapting to my context, I have created different variables (eg users and data) which were meant to do the same thing, except some methods were calling data and some were calling users. This is an easy mistake to do as you try to understand other's code and java functionality (it happens to me all the time).

It's good to know that you can println in your console anywhere in your java code, so you can debug like that pretty easily.

Good luck!

查看更多
▲ chillily
7楼-- · 2019-01-14 05:52

I had this same issue where neither onCreateViewHolder or onBindViewHolder were being called. Changing your onCreateViewHolder method to use the Context you saved a reference to in the constructor instead of doing parent.getContext() should fix it:

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.d(TAG, "onCreateViewHolder called");
    View view = LayoutInflater.from(mContext).inflate(R.layout.row_resource_efficiency, parent, false);
    return new ViewHolder(view);
查看更多
登录 后发表回答