Recyclerview changing items when is scrolled

后端 未结 6 777
一个人的身影
一个人的身影 2021-02-09 17:57

Can someone explain to me, what the problem is?

When I scroll down and then back to the top, my items were changed for other items in the list, and if I scroll again, th

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-09 18:37

    Try to initialize your view on RecyclerView.ViewHolder and inflate your layout on onCreateViewHolder() and don't inflate again the layout in onBindViewHolder().

    public class AcademicAdapter extends RecyclerView.Adapter {
        private LayoutInflater inflater;
        private List data;
    
    public AcademicAdapter(List data) {
        this.data = data;
    }
    
    @Override
    public AcademicVH onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_academic_level, parent, false);
        AcademicVH mViewHold = new ViewHolder(mView);
        return mViewHold;
    }
    
    @Override
    public void onBindViewHolder(AcademicVH holder, int position) {
         final AcademicStatus mItems=this.data.get(position);
    
    
        holder.txtSubject.setText(mItems.getSubject());
        holder.txtGroup.setText(mItems.getGroup());
        holder.txtStatus.setText(mItems.getStatus());
        holder.txtFinalNote.setText(mItems.getFinalNote());
        holder.txtYear.setText(mItems.getAcademicYear());
    
    }
    
    @Override
    public int getItemCount() {
        return data.size();
    }
    
    public static class AcademicVH extends RecyclerView.ViewHolder {
        public LinearLayout containerData;
        public TextView txtSubject,txtGroup,txtStatus,txtFinalNote,txtYear;
    
        public AcademicVH(View itemView) {
            super(itemView);
    
            containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
            txtSubject = (TextView) itemView.findViewById(R.id.txtSubjectValue);
            txtGroup = (TextView) itemView.findViewById(R.id.txtGroupValue);
            txtStatus = (TextView) itemView.findViewById(R.id.txtStatusValue);
            txtFinalNote = (TextView) itemView.findViewById(R.id.txtFinalNoteValue);
            txtYear = (TextView) itemView.findViewById(R.id.txtYearValue);
        }
    }
    }
    

提交回复
热议问题