我正在尝试在viewType
上使用ViewHolder
将标题与其他项目分开。使用我目前的代码,标题显示正确,但下面没有显示其他项目。如果我删除viewType
条件,则所有项都会正确显示,但当然,标题没有所需的视图。
这是我的适配器代码:
public class GradelistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int ITEM = 1, HEADER = 0;
private List<ItemGrade> gradeList;
public class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView course, grade;
public ItemViewHolder(View view) {
super(view);
course = (TextView) view.findViewById(R.id.course);
grade = (TextView) view.findViewById(R.id.grade);
}
}
public class HeaderViewHolder extends RecyclerView.ViewHolder {
public TextView header;
public HeaderViewHolder(View view) {
super(view);
header = (TextView) view.findViewById(R.id.year);
}
}
public GradelistAdapter(List<ItemGrade> gradelist) {
this.gradeList = gradelist;
}
@Override
public int getItemViewType(int position) {
if (gradeList.get(position).getGrade() == -1) {
return HEADER;
}
return ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == HEADER) {
View header = inflater.inflate(R.layout.gradelist_header, parent, false);
viewHolder = new HeaderViewHolder(header);
}
else{
View item = inflater.inflate(R.layout.gradelist_row, parent, false);
viewHolder = new ItemViewHolder(item);
}
Log.d("onCreateViewHolder", Integer.toString(viewType));
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ItemGrade entry = gradeList.get(position);
if (holder.getItemViewType() == HEADER) {
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
headerViewHolder.header.setText("Viti " + entry.getYear());
} else {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.course.setText(entry.getCourse());
itemViewHolder.grade.setText(Integer.toString(entry.getGrade()));
}
}
@Override
public int getItemCount() {
return gradeList.size();
}
答案 0 :(得分:0)
这很令人尴尬。一切都很好,只是我的标题布局填满整个屏幕,我需要向下滚动才能看到列表的其余部分。