有人能解释一下我的问题吗?
当我向下滚动然后回到顶部时,我的项目已被列表中的其他项目更改,如果我再次滚动,则项目又会更改。
这是我的适配器。
public class AcademicAdapter extends RecyclerView.Adapter<AcademicAdapter.AcademicVH> {
private LayoutInflater inflater;
private List<AcademicStatus> data;
public AcademicAdapter(List<AcademicStatus> data) {
this.data = data;
}
@Override
public AcademicVH onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.row_academic_level, parent, false);
return new AcademicVH(view);
}
@Override
public void onBindViewHolder(AcademicVH holder, int position) {
View view = inflater.inflate(R.layout.row_container_academic_level, holder.containerData, false);
TextView txtSubject = (TextView) view.findViewById(R.id.txtSubjectValue);
TextView txtGroup = (TextView) view.findViewById(R.id.txtGroupValue);
TextView txtStatus = (TextView) view.findViewById(R.id.txtStatusValue);
TextView txtFinalNote = (TextView) view.findViewById(R.id.txtFinalNoteValue);
TextView txtYear = (TextView) view.findViewById(R.id.txtYearValue);
txtSubject.setText(data.get(position).getSubject());
txtGroup.setText(data.get(position).getGroup());
txtStatus.setText(data.get(position).getStatus());
txtFinalNote.setText(data.get(position).getFinalNote());
txtYear.setText(data.get(position).getAcademicYear());
holder.containerData.addView(view);
}
@Override
public int getItemCount() {
return data.size();
}
public static class AcademicVH extends RecyclerView.ViewHolder {
private LinearLayout containerData;
public AcademicVH(View itemView) {
super(itemView);
containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
}
}
}
onBindViewHolder
中应该重用holder
,而不是重新inflate
一个新的。同时,将findViewById
等视图操作移动到AcademicVH
中。示例 - tianyu