这是我的 RecyclerViewAdapter 类,非常感谢:
公共类RecyclerViewAdapter扩展了RecyclerView.Adapter { 上下文mContext; 列出mData;
public RecyclerViewAdapter(Context mContext, List<Contact> mData) {
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v;
v = LayoutInflater.from(mContext).inflate(R.layout.item_contact, viewGroup, false);
MyViewHolder viewHolder = new MyViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int position) {
myViewHolder.tvName.setText(mData.get(position).getName());
myViewHolder.tvFname.setText(mData.get(position).getFname());
myViewHolder.img.setImageResource(mData.get(position).getPhoto());
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
private TextView tvName;
private TextView tvFname;
private ImageView img;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvName = (TextView)itemView.findViewById(R.id.tvName);
tvFname = (TextView)itemView.findViewById(R.id.tvFname);
img = (ImageView)itemView.findViewById(R.id.img_contact);
}
}
}