本文介绍了RecyclerView显示的TextView在顶端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用RecyclerView与CardView显示卡不同的文字,看起来像这样
I am using RecyclerView with CardView to display different text in cards that looks like this
但我想在顶部有一个文本视图显示只有一个这样的时间
But I want a Text View at the top that displays only one time like this
我应该做一个独立布局的TextView并调用它在相同的活动?我认为,不推荐
Should I make a separate layout with TextView and call it in same Activity? I think that's not recommended
推荐答案
你应该尝试的适配器内部的getItemType
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;
}
}
这篇关于RecyclerView显示的TextView在顶端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!