为了账号安全,请及时绑定邮箱和手机立即绑定

Recyclerview 不显示项目?

Recyclerview 不显示项目?

繁星淼淼 2023-10-19 15:01:52
我想在 上显示下面的布局RecyclerView。此布局根据商店数量而重复。假设我们有 2 家商店,那么用户将看到两种布局,具有不同的商店名称。适配器类: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.size() != 0 ? objects.size() : 0;    }    class ViewHolder extends RecyclerView.ViewHolder {//        @BindView(R.id.rlSchedule)//        RelativeLayout rlSchedule;        @BindView(R.id.tvDeliveryScheduleLabel)        TextView tvDeliveryScheduleLabel;//        @BindView(R.id.tvDeliverySchedule)//        TextView tvDeliverySchedule;        public ViewHolder(View itemView) {            super(itemView);            itemView.setOnClickListener(v -> {                if (listener!=null && getAdapterPosition() != RecyclerView.NO_POSITION) {                    listener.onItemClick(objects.get(getAdapterPosition()));                }            });        }
查看完整描述

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()));

                    }

                });

     }


查看完整回答
反对 回复 2023-10-19
?
扬帆大鱼

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");

        }

    }

}


查看完整回答
反对 回复 2023-10-19
  • 2 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信