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条回答
小情绪 Triste *
2楼-- · 2019-01-14 05:53

In my case I set the ID of my RecyclerView to "recyclerView". It seems that this ID was already defined somewhere, because when I changed that to different ID, the methods from the adapter were called properly.

Link to the answer

查看更多
女痞
3楼-- · 2019-01-14 05:57

In my case a had a Fragment with RecyclerView inside a ViewPager. The issue appeared because i used Fragment.getFragmentManager() instead of Fragment.getChildFragmentManager for my FragmentStatePagerAdapter's constructor.

P.S. Using Layout Inspector might be a great help at debugging this sort of issue.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-14 05:59

If you put wrap_content as height of your list as well as the height of your item list, nothing will show up and none of your recyclerView functions will be called.

查看更多
登录 后发表回答