设置RecyclerView标头而不覆盖第一个元素

时间:2018-06-07 02:03:29

标签: java android android-recyclerview recycler-adapter

你好我有一个recyclerView,它显示包含从mysql中获取的数据并带有改进的卡片。在我的适配器中,我能够将第一张卡设置为静态元素,该元素是用户的个人资料图片,如接受的答案here所示。但是他们没有注意到该方法会覆盖前一个元素(位置0处的元素)。第一个元素被静态视图替换,但我不希望这样。我想要

static card,
cardview 0,
cardview 1,
cardview 2,
etc

有什么想法吗?感谢

2 个答案:

答案 0 :(得分:0)

在'RecyclerView.Adapter'中使用'viewType'。

RecyclerView中的onCreateViewHolder

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    int layout = -1;
    switch (viewType) {
        case RecyclerViewItem.USER_PROFILE:
            layout = R.layout.driving_item;
            break;
        case RecyclerViewItem.OTHER_USER_PROFILE:
            layout = R.layout.date_item;
            break;
        case RecyclerViewItem.DATE:
            layout = R.layout.send_button_item;
            break;
    }

    View v = LayoutInflater.from(context).inflate(layout, parent, false);
    return new MyViewHolder(v);
}

RecyclerView中的onBindViewHolder

@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int position) {
    final int pos = myViewHolder.getAdapterPosition();
    if (pos == -1) {
        return;
    }
    final RecyclerViewItem item = ArrayList.get(pos);

    switch (item.getType()) {
        case RecyclerViewItem.USER_PROFILE:
            //By type you want to do differently.
            break;
        case RecyclerViewItem.OTHER_USER_PROFILE:
            //By type you want to do differently.
            break;
        case RecyclerViewItem.DATE:
            //By type you want to do differently.
            break;
    }
}

如果您有任何其他问题,请通过评论告诉我。

答案 1 :(得分:0)

没有像listview.addHeaderView()这样的简单方法,但您可以通过在适配器中为标题添加类型来实现此目的。

这是一个例子

public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    String[] data;

    public HeaderAdapter(String[] data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            //inflate your layout and pass it to view holder
            return new VHItem(null);
        } else if (viewType == TYPE_HEADER) {
            //inflate your layout and pass it to view holder
            return new VHHeader(null);
        }

        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) {
            String dataItem = getItem(position);
            //cast holder to VHItem and set data
        } else if (holder instanceof VHHeader) {
            //cast holder to VHHeader and set data for header.
        }
    }

    @Override
    public int getItemCount() {
        return data.length + 1;
    }

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

        return TYPE_ITEM;
    }

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

    private String getItem(int position) {
        return data[position - 1];
    }

    class VHItem extends RecyclerView.ViewHolder {
        TextView title;

        public VHItem(View itemView) {
            super(itemView);
        }
    }

    class VHHeader extends RecyclerView.ViewHolder {
        Button button;

        public VHHeader(View itemView) {
            super(itemView);
        }
    }
}