这是我在RecycleView
适配器类中使用的代码。我不知道这是使用视图绑定的正确方法。如果您有更好的解决方案,请回答我。谢谢你。
@Override
public CategoryAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.common_circle_image, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CategoryAdapter.MyViewHolder holder, final int position) {
holder.binding.img.setBackgroundResource(addAdapterData.get(position).getItemUrl());
holder.binding.txt.setText(addAdapterData.get(position).getItemName());
}
@Override
public int getItemCount() {
return addAdapterData.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
CommonCircleImageBinding binding;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
binding = CommonCircleImageBinding.bind(itemView);
binding.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
commonItemClick.onItemClick(getAdapterPosition(),"");
}
});
}
}
另外,我想知道在同一个班级中使用是否R.layout.layout_name
正确ViewBinding
。