带标题的Recylerview显示数据,但负1

时间:2018-11-27 04:31:34

标签: android

所以我有3条记录,但是当我尝试通过我的android应用程序显示它时,它只显示2条记录。

这是我的recylerview的适配器

   public class Sales_header_adapter  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    private List<Sales_header_model> salesheaderlist;

    public Sales_header_adapter(List<Sales_header_model> salesheaderlist) {
        this.salesheaderlist = salesheaderlist;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View VHItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_header_rec, parent, false);
            return new VHItem(VHItem);
        } else if (viewType == TYPE_HEADER) {
            View VHHeader = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_header_rec_header, parent, false);
            return new VHHeader(VHHeader);
        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VHItem) {
            Sales_header_model as =  salesheaderlist.get(position);
            VHItem VHitem = (VHItem)holder;
            VHitem.nonota.setText(as.getNoNota());
            VHitem.tanggal.setText(as.getTanggalTransaksi());
            VHitem.total.setText("Rp. 1.000.000");

        } else if (holder instanceof VHHeader) {
            Sales_header_model dataItem = getItem(position);
            VHHeader VHheader = (VHHeader)holder;
            VHheader.nonota_h.setText("No Nota");
            VHheader.tanggal_h.setText("Tanggal");
            VHheader.total_h.setText("Total");
        }
    }

    @Override
    public int getItemCount() {
        return salesheaderlist.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;
        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position  == -1;
    }

    private Sales_header_model getItem(int position) {
        return salesheaderlist.get(position);
    }

    class VHItem extends RecyclerView.ViewHolder {
        public TextView  tanggal, total,nonota;
        public VHItem(View itemView) {
            super(itemView);
            tanggal         =  itemView.findViewById(R.id.tanggalnota);
            total           =  itemView.findViewById(R.id.total);
            nonota          =  itemView.findViewById(R.id.nonota);
        }
    }

    class VHHeader extends RecyclerView.ViewHolder {
        public TextView  tanggal_h, total_h,nonota_h;
        public VHHeader(View itemView) {
            super(itemView);
            tanggal_h               = itemView.findViewById(R.id.tanggalnota_h);
            total_h                 = itemView.findViewById(R.id.total_h);
            nonota_h                = itemView.findViewById(R.id.nonota_h);
        }
    }
}
从我的getItemCount

我得到3。提前谢谢,我该怎么解决。参考Is there an addHeaderView equivalent for RecyclerView?

1 个答案:

答案 0 :(得分:1)

执行此操作:

private boolean isPositionHeader(int position) {
        return position == 0;
    }

private Sales_header_model getItem(int position) {
    return salesheaderlist.get(position - 1);
}

而不是:

private boolean isPositionHeader(int position) {
        return position  == -1;
    }
private Sales_header_model getItem(int position) {
    return salesheaderlist.get(position);
}