Jacques Krause
Jacques Krause

Reputation: 5569

RecyclerView Display TextView At Top

I am using RecyclerView with CardView to display different text in cards that looks like this

enter image description here

But I want a Text View at the top that displays only one time like this

enter image description here

Should I make a separate layout with TextView and call it in same Activity? I think that's not recommended

Upvotes: 0

Views: 1020

Answers (1)

florent champigny
florent champigny

Reputation: 979

you should try the getItemType inside the adapter

private static final int TYPE_HEADER = 0;
private static final int TYPE_CELL = 1;

@Override
public int getItemViewType(int position) {
    if (position == 0)
        return TYPE_HEADER;
    else
        return TYPE_CELL;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View convertView;
    switch (viewType) {
        case TYPE_HEADER:
            convertView = LayoutInflater.from(context).inflate(R.layout.myTextView, parent, false);
            return new MyTextViewViewHolder(convertView);
        case TYPE_CELL:
            convertView = LayoutInflater.from(context).inflate(R.layout.myCell, parent, false);
            return new CellViewHolder(convertView);
    }
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (getItemViewType(position)) {
        case TYPE_HEADER:
            ((MyTextViewViewHolder) holder) ...
            break;
        case TYPE_CELL:
            ((CellViewHolder) holder) ...
            break;
    }
}

Upvotes: 1

Related Questions