Skip to content Skip to sidebar Skip to footer

Recyclerview Is Duplicating Items

My recyclerview is duplicating items when I roll it and I'm already calling adapter.notifyDataSetChanged(). So, probably I'm calling data set update at wrong place, but I can't fin

Solution 1:

Use holder.setIsRecyclable(false) in onCreateViewHolder()

It worked for me.

Solution 2:

I replicated your problem.

adding holder.priceGroup.removeAllViews(); on onBindViewHolder will fix it. Like so:

@OverridepublicvoidonBindViewHolder(ChecklistAdapter.ViewHolder holder,
                             int position) {
    ChecklistpackageModel= packageList.get(position);
    holder.packageName.setText(packageModel.getTitle());

    intid= (position+1)*100;
    holder.priceGroup.removeAllViews();

    for(String price : packageModel.getQuestions()){
        RadioButtonrb=newRadioButton(ChecklistAdapter.this.context);
        rb.setId(id++);
        rb.setText(price);
        holder.priceGroup.addView(rb);
    }
}

You were adding to the view every time without removing the previous views.

Solution 3:

you have to setHasStableIds(true) to your Adapter in your activity.

and in your Adapter class you have to set below method. It may help you. it is work for me.

@OverridepubliclonggetItemId(int position) {
        return position;
    }

    @OverridepublicintgetItemViewType(int position) {
        return position;
    }


    @OverridepublicvoidsetHasStableIds(boolean hasStableIds) {
        super.setHasStableIds(hasStableIds);
    }

you want like this?

enter image description here

Solution 4:

You just need to call list.clear(); before insert into RecyclerViewAdapter. then call after call adapter.notifyDatasetChange()

Solution 5:

Put holder.setIsRecyclable(false) in onBindViewHolder() like below example.

@Override
    publicvoidonBindViewHolder(UsageStatVH holder, int position) {
        holder.bindTo(list.get(position));
        holder.setIsRecyclable(false);
    }

Post a Comment for "Recyclerview Is Duplicating Items"