2 回答
TA贡献1963条经验 获得超6个赞
在您的 ViewHolder 类中添加以下内容进行这些更改 -
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, view); // To bind view with ButterKnife
itemView.setOnClickListener(v -> {
if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {
listener.onItemClick(objects.get(getAdapterPosition()));
}
});
}
TA贡献1799条经验 获得超9个赞
我认为你的适配器没问题。但是,在该getItemCount函数中您需要检查null. 我按如下方式修改了适配器,并让我知道这是否有效。
public class DeliverySlotsAdapter extends RecyclerView.Adapter<DeliverySlotsAdapter.ViewHolder> {
private Context context;
LayoutInflater layoutInflater;
List<Object> objects;
private LayoutInflater inflater;
private CallbackListener.SimpleCallback listener;
public DeliverySlotsAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
this.inflater = LayoutInflater.from(context);
}
public void setListener(CallbackListener.SimpleCallback listener) {
this.listener = listener;
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
RecyclerView.ViewHolder viewHolder;
View view = inflater.inflate(R.layout.item_checkout_delivery_slots, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.bind(objects.get(position));
}
@Override
public int getItemCount() {
return objects != null && objects.size() > 0 ? objects.size() : 0;
}
class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tvDeliveryScheduleLabel)
TextView tvDeliveryScheduleLabel;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(v -> {
if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {
listener.onItemClick(objects.get(getAdapterPosition()));
}
});
}
public void bind(Object object) {
DeliverySlotItem deliverySlotItem = (DeliverySlotItem) object;
tvDeliveryScheduleLabel.setText(deliverySlotItem.shop_name + " Delivery");
}
}
}
添加回答
举报