I’m using a listview that uses ViewHolder pattern and get advantage of caching mechanism. Here is a code snippet that I try to set one of my list item’s background
if(drawable!=null)
{
if(this.backgroundBurnColor!=null && this.backgroundBurnColor.getValue()!=0)
{
Log.d("burncolor", this.backgroundBurnColor.getValue()+"");
drawable.setColorFilter(this.backgroundBurnColor.getValue(), Mode.MULTIPLY);
}else if(this.burnColorKey!=null)
{
Log.d("burncolor", this.Owner.getOwner().getColors().get(this.burnColorKey).getValue()+"");
drawable.setColorFilter(this.Owner.getOwner().getColors().get(this.burnColorKey).getValue(), Mode.MULTIPLY);
}else{
drawable.setColorFilter(null);
}
v.setBackgroundDrawable(drawable);
}
In this code the drawable is for the same item type, but even if the same item type when I change drawable image it changes as I want. But changing color filter of the drawable changes every drawables filter in the list part appearing on screen. For example when I scroll down the list and a different color filter appears on the screen, then all drawables’ color filters turn to the same filter, I need to get the seperate color filter for my each row. Any help? Thanks
For those who wonders about the answer,
There is a blog post by Romain Guy about drawable mutations in android :
http://www.curious-creature.org/2009/05/02/drawable-mutations/
When drawables are referenced from the resources, only one instance of a Drawable initialized, so all references referring to the same object. for this there is a
mutate()method for drawablesInstead of writing
writing
will solve the problem