姬長信(Redy)

java – RecyclerView不调用onCreateViewHolder或on…


没有得到任何错误,所有数据似乎都有效.出于某种原因,正在调用与视图相关的方法.我确定了以下内容:

> getItemCount()是唯一被调用的适配器方法,并返回一个正整数值,(我知道这将是你们要看的区域)

>正在调用构造函数,成员变量有效.
>父视图是一个垂直的LinearLayout;没有滚动视图,或任何其他具有自己的滚动属性的视图.
>创建包含片段视图并在屏幕上显示.

这是片段中的声明,后跟适配器.任何帮助将不胜感激,因为这是完全困惑的.

SubMenuAdapter adapter = new SubMenuAdapter(getActivity(), mContentItems);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);
public class SubMenuAdapter extends RecyclerView.Adapter {
private static final String TAG = String.format("==> %S", SubMenuAdapter.class.getSimpleName());

private final List mContentItems;
private Context mContext;

public SubMenuAdapter(Context context, List 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);
    }
}