我有以下适配器,它使用包含一个TextView的CardView项填充RecyclerView。它使用String数组中的数据填充这些TextView。但是,现在我想在我的CardViews中添加另一个TextView并用一个单独的数据阵列填充它,我想知道如何才能最好地实现这一点:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.grade);
}
}
public RecyclerAdapter(String[] myDataset) {
mDataset = myDataset;
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
所有帮助一如既往地受到赞赏!
答案 0 :(得分:3)
只需在CardLayout XML TextView
R.layout.card_item
或任何其他视图
然后
public TextView mTextView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.grade);
mTextView1 = (TextView)itemView.findViewById(R.id.grade1);
mTextView2 = (TextView)itemView.findViewById(R.id.grade2);
mTextView3 = (TextView)itemView.findViewById(R.id.grade3);
}
最后将数据设置为所有想要的文本视图
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText("String");
holder.mTextView1.setText("String");
holder.mTextView2.setText("String");
holder.mTextView3.setText("String");
}