Skip to content Skip to sidebar Skip to footer

Recycler View Item Colour Change Repeating After Scrolling

Recycler view item color change repeating after scrolling. I used to change color at a particular position of the Recyclerview list. When scrolling occurs another item at the botto

Solution 1:

The recycler view recycles the view in OnBindViewHolder.So when items are clicked it gets reflected in some other positions.To solve this. create a global SparseBooleanArray to store the clicked position.

privatefinal SparseBooleanArray array=new SparseBooleanArray();

Then inside final viewholder add the clickListener and onClick store the position of the clicked item.

publicclassViewHolderextendsRecyclerView.ViewHolder {
    public YOURVIEW view;
    publicViewHolder(View v) {
        super(v);
        view = (YOURVIEW) v.findViewById(R.id.YOURVIEWID);
        view.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                array.put(getAdapterPosition(),true);
                notifyDataSetChanged();
            }
        });
    }
}

And in inside OnBindViewHolder,

@OverridepublicvoidonBindViewHolder(final ViewHolder holder, finalint position) {
    if(array.get(position)){
        holder.mainlayout.setBackgroundColor(Color.parseColor("#e927a4d1"));
    }else{
        holder.mainlayout.setBackgroundColor(UNSELECTEDCOLOR);
    }
}

Solution 2:

It is due to recycling of viewholder of recycler view. Try this It worked for me.

publicListViewHolder(View itemView) {
        super(itemView);

        this.setIsRecyclable(false);


    }

Solution 3:

I think you can set your background color in void onBindViewHolder(VH holder, int position); such as

List<Integer> selectedPosition = new ArrayList(yourDataSize);
voidonBindViewHolder(VH holder, int position){

     if(selectedPosition.get(position) == 1){
       holder.mainlayout.setBackgroundColor(Color.parseColor("#e927a4d1"));
     }else {
       holder.mainlayout.setBackgroundColor(normalColor);
     }

     //when the item clicked 
     selectedPosition.add(position,1);

}

Solution 4:

That function returns relative position not absolute so when screen is scrolled position is replaced with a new value. use position from your list for the desired result.

Solution 5:

try to implement this method in your adapter class, may be it's solve your problem

@OverridepublicvoidonViewRecycled(ViewHolderProduct holder) {
        super.onViewRecycled(holder);
        holder.mainlayout.removeAllViews();
} 

Post a Comment for "Recycler View Item Colour Change Repeating After Scrolling"