RecylerView项目未显示

时间:2019-07-20 00:17:33

标签: java android android-recyclerview recycler-adapter

我正在尝试在应用程序中实现RecyclerView。我按照android配置单元指南进行操作,但项目不会显示。经过大量检查后,我仍然找不到问题。

我是否需要在build.app中使用实现RecylerView,我正在使用androidX 我正在片段活动中使用此RecylcerView

我的适配器类:

public class CouponsAdapter extends RecyclerView.Adapter<CouponsAdapter.ViewHolder> {
    private List<CouponsModel> couponsList;

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView productIds,couponUsage,couponLimit,couponAmount,couponCode,couponType,date;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            productIds=(TextView)itemView.findViewById(R.id.products_ids);
            couponUsage=(TextView)itemView.findViewById(R.id.usage_limit);
            couponAmount=(TextView)itemView.findViewById(R.id.coupon_amount);
            couponCode=(TextView)itemView.findViewById(R.id.coupon_code);
            couponType=(TextView)itemView.findViewById(R.id.coupon_type);
            date=(TextView)itemView.findViewById(R.id.date);

        }

    }

public CouponsAdapter (List<CouponsModel> couponsList){
        this.couponsList=couponsList;


}
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.coupons_list,parent,false);


        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        CouponsModel couponsModel=couponsList.get(position);
        holder.date.setText(couponsModel.getDate());
        holder.couponType.setText(couponsModel.getCouponType());
        holder.couponCode.setText(couponsModel.getCouponCode());
        holder.couponAmount.setText(couponsModel.getCouponAmount());
        holder.couponUsage.setText(couponsModel.getCouponUsage());
        holder.productIds.setText(couponsModel.getProductIds());

    }

    @Override
    public int getItemCount() {
        if(couponsList.size() == 0)
            return 1;
        return couponsList.size();
    }

我的片段:

  // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_coupons, container, false);
        recyclerView=view.findViewById(R.id.coupons_recyler_view);
        couponsAdapter=new CouponsAdapter(couponsModelList);
        recyclerView.setAdapter(couponsAdapter);

        int x=32;
        couponsModelList.add(new CouponsModel(x,x,x,x,"free50","free",x));
        couponsAdapter.notifyDataSetChanged();
        return view;

    }

2 个答案:

答案 0 :(得分:1)

您应将布局管理器设置为RecyclerView。

如果未设置布局管理器,则会出现以下错误:未附加布局管理器;跳过布局

答案 1 :(得分:0)

您缺少布局管理器。只需将此行添加到代码中即可。

recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));