带页眉和页脚的Recyclerview

时间:2018-12-22 13:37:15

标签: android-recyclerview sectionedrecyclerviewadapter

enter image description here部门数量 name1 1 10.00在此处输入图片描述 名称2 2 10.00 名称3 3 50.00

总计6 70.00

部门数量 名称4 2 10.00 名称5 5 20.00 名称6 4 30.00

总计11 60.00

我在API 1(名称1到名称3)和API2(名称4到名称6)中都具有api数据,但我曾尝试过,但标头将从正常项目移至0位置并出现在项目单元格中

任何人都可以帮助我获得页眉,页脚获得recyclerview吗?

部分标题视图

公共类CallDetailsAdapter扩展了RecyclerView.Adapter {

private List<Object> mList;
private Context mContext;
private static final int ITEM_TYPE_NORMAL = 0;
private static final int ITEM_TYPE_HEADER = 1;
private static final int VIEW_PROG = 2;
private ArrayList<String> displayedTicketList;
private Fragment fragment;

public CallDetailsAdapter(Context mContext, Fragment fragment, List<Object> mList) {
    this.mContext = mContext;
    this.fragment = fragment;
    this.mList = mList;
    displayedTicketList = new ArrayList<>();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder = null;
    if (viewType == ITEM_TYPE_HEADER) {
        viewHolder = new SectionViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_header_sales, parent, false));
    } else if (viewType == ITEM_TYPE_NORMAL) {
        viewHolder = new DashboardViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_sales_summary_list, parent, false));
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()) {
        case ITEM_TYPE_HEADER:
            SectionViewHolder mSectionViewHolder = (SectionViewHolder) holder;
            //mSectionViewHolder.txtTitleDepartment.setText("Department");
            break;
        case ITEM_TYPE_NORMAL:
            DashboardViewHolder mViewHolder = (DashboardViewHolder) holder;
            ModelSalesSummary model = (ModelSalesSummary) mList.get(position);
            mViewHolder.txtDeptName.setText("" + model.getDepartmentName());
            mViewHolder.txtQty.setText("" + model.getQty());
            mViewHolder.txtAmount.setText("" + model.getAmount());
            break;
    }
}

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

@Override
public int getItemViewType(int position) {
    if (isPositionHeader(position))
        return ITEM_TYPE_HEADER;
    return ITEM_TYPE_NORMAL;
}

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

private interface ItemClickListener {
    void onItemClicked(int position);
}

static class SectionViewHolder extends RecyclerView.ViewHolder {
    SectionViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
    }
}

static class DashboardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    @BindView(R.id.txtDeptName)
    TextView txtDeptName;
    @BindView(R.id.txtQty)
    TextView txtQty;
    @BindView(R.id.txtAmount)
    TextView txtAmount;
    private ItemClickListener mItemClickListener;

    DashboardViewHolder(View view) {
        super(view);
        this.mItemClickListener = mItemClickListener;
        ButterKnife.bind(this, view);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        mItemClickListener.onItemClicked(getAdapterPosition());
    }
}

}

部门数量 名称1 1 10.00 名称2 2 10.00 名称3 3 50.00

总计6 70.00

部门数量 名称4 2 10.00 名称5 5 20.00 名称6 4 30.00

总计11 60.00

enter image description here

0 个答案:

没有答案