Recyclerview changing items when is scrolled

后端 未结 6 789
一个人的身影
一个人的身影 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:24

    Problem is with library compile 'com.android.support:recyclerview-v7:25.0.0'. Try to use compile 'com.android.support:recyclerview-v7:23.1.1'.

    0 讨论(0)
  • 2021-02-09 18:25

    you define your item in wrong place, define your View inside of view holder and like below

    public static class AcademicVH extends RecyclerView.ViewHolder {
    private LinearLayout containerData;
    
    public AcademicVH(View itemView) {
        super(itemView);
        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);
        containerData = (LinearLayout) itemView.findViewById(R.id.containerData);
        }
    }
    

    another trick, when you use condition (if) in your onBindViewHolder, you should complete it with (else).

    0 讨论(0)
  • 2021-02-09 18:28

    Recyclerview shuffling issue understanding of the concept behind

      @Override
      public void onBindViewHolder(final MyViewHolder holder, int position) {
          holder.setIsRecyclable(false);
      }
    
     @Override
     public long getItemId(int position) 
     {
            return position;
     }
    
     @Override
     public int getItemViewType(int position) 
     {
            return position;
     }
    

    Add setHasStableIds(true); in your adapter constructor and Override these two methodes in adapter.

    0 讨论(0)
  • 2021-02-09 18:33

    Just put you recylerView in a NestedScroll View in your xml and add the property nestedScrollingEnabled = false.

    And on your adapter onBindViewHolder add this line

    final AcademicVH viewHolder = (AcademicVH)holder;

    Use this viewHolder object with your views to setText or do any kind of Click events.

    e.g viewHolder.txtSubject.setText("Example");

    0 讨论(0)
  • 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<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) {
    
        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);
        }
    }
    }
    
    0 讨论(0)
  • 2021-02-09 18:40

    Add holder.setIsRecyclable(false); in your onBindViewHolder

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
    }
    
    0 讨论(0)
提交回复
热议问题