本文介绍了RecyclerView 不调用 onCreateViewHolder 或 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() 是唯一被调用的适配器方法,它返回一个正整数值,(我知道这将是你们会关注的领域)

  • 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 是一个垂直的 LinearLayout;没有滚动视图,或任何其他具有自己滚动属性的视图.

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

推荐答案

这也可能对某人有帮助

第一次使用

recyclerView.setAdapter(adapter);

然后:

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

所以它看起来像这样:

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

顺序颠倒了

更新:

现在我只是使用:

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

在xml中的RecyclerView

这篇关于RecyclerView 不调用 onCreateViewHolder 或 onBindView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:14