RecyclerView在顶部显示TextView

时间:2015-06-15 08:16:37

标签: android android-recyclerview

我正在使用带有CardView的RecyclerView在看起来像这样的卡片中显示不同的文字

enter image description here

但我希望顶部的文字视图只显示一次这样的

enter image description here

我应该使用TextView创建单独的布局并在同一个Activity中调用它吗?我认为不推荐

1 个答案:

答案 0 :(得分:1)

你应该在适配器中尝试getItemType

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;
    }
}