我试图使用RecyclerView显示一组值,我试图使用数据绑定,但是RecyclerView没有显示任何项目,并且它为空。
如果我不使用数据绑定,则代码可以正常工作,并且Recycerview显示这些项目。
下面是适配器类
public class ParticipantsAdapter extends RecyclerView.Adapter<ParticipantsAdapter.ParticipantsViewHolder> {
ArrayList nameList,iconList;
public ParticipantsAdapter(ArrayList name, ArrayList iconList) {
this.nameList = name;
this.iconList = iconList;
}
@NonNull
@Override
public ParticipantsAdapter.ParticipantsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
ParticpantItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),R.layout.particpant_item,null,false);
return new ParticipantsViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull
ParticipantsAdapter.ParticipantsViewHolder participantsViewHolder, int i) {
participantsViewHolder.bindTo(i);
}
@Override
public int getItemCount() {
return nameList.size();
}
class ParticipantsViewHolder extends RecyclerView.ViewHolder {
private ParticpantItemBinding binding;
TextView name;
ImageView icon;
public ParticipantsViewHolder(ParticpantItemBinding view) {
super(view.getRoot());
this.binding = view;
this.binding.executePendingBindings();
}
private void bindTo(int pos) {
binding.tvName.setText(nameList.get(pos)+"");
binding.ivAvtar.setImageResource((int)iconList.get(pos));
binding.executePendingBindings();
}
}