Recyclerview 不显示项目?

我想在 上显示下面的布局RecyclerView。此布局根据商店数量而重复。假设我们有 2 家商店,那么用户将看到两种布局,具有不同的商店名称。


https://img1.sycdn.imooc.com/6530d4920001d8ee03880072.jpg

适配器类:


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

                }

            });

        }



繁星淼淼
浏览 98回答 2
2回答

神不在的星期二

在您的 ViewHolder 类中添加以下内容进行这些更改 -&nbsp; &nbsp; public ViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ButterKnife.bind(this, view);&nbsp; // To bind view with ButterKnife&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemView.setOnClickListener(v -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.onItemClick(objects.get(getAdapterPosition()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp;}

扬帆大鱼

我认为你的适配器没问题。但是,在该getItemCount函数中您需要检查null. 我按如下方式修改了适配器,并让我知道这是否有效。public class DeliverySlotsAdapter extends RecyclerView.Adapter<DeliverySlotsAdapter.ViewHolder> {&nbsp; &nbsp; private Context context;&nbsp; &nbsp; LayoutInflater layoutInflater;&nbsp; &nbsp; List<Object> objects;&nbsp; &nbsp; private LayoutInflater inflater;&nbsp; &nbsp; private CallbackListener.SimpleCallback listener;&nbsp; &nbsp; public DeliverySlotsAdapter(Context context, List<Object> objects) {&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; this.objects = objects;&nbsp; &nbsp; &nbsp; &nbsp; this.inflater = LayoutInflater.from(context);&nbsp; &nbsp; }&nbsp; &nbsp; public void setListener(CallbackListener.SimpleCallback listener) {&nbsp; &nbsp; &nbsp; &nbsp; this.listener = listener;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {&nbsp; &nbsp; &nbsp; &nbsp; RecyclerView.ViewHolder viewHolder;&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.item_checkout_delivery_slots, parent, false);&nbsp; &nbsp; &nbsp; &nbsp; return new ViewHolder(view);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onBindViewHolder(final ViewHolder holder, final int position) {&nbsp; &nbsp; &nbsp; &nbsp; holder.bind(objects.get(position));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int getItemCount() {&nbsp; &nbsp; &nbsp; &nbsp; return objects != null && objects.size() > 0 ? objects.size() : 0;&nbsp; &nbsp; }&nbsp; &nbsp; class ViewHolder extends RecyclerView.ViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; @BindView(R.id.tvDeliveryScheduleLabel)&nbsp; &nbsp; &nbsp; &nbsp; TextView tvDeliveryScheduleLabel;&nbsp; &nbsp; &nbsp; &nbsp; public ViewHolder(View itemView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(itemView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemView.setOnClickListener(v -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listener.onItemClick(objects.get(getAdapterPosition()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void bind(Object object) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DeliverySlotItem deliverySlotItem = (DeliverySlotItem) object;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tvDeliveryScheduleLabel.setText(deliverySlotItem.shop_name + " Delivery");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java