Reputation: 17671
I have a ShapeDrawable drawable/my_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#FFFF00" />
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp" />
</shape>
which I use as background of some TextView
s at several places in my app. Like in layout/my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout...>
<TextView
android:id="@+id/tag"
android:background="@drawable/my_drawable"
android:layout_height="wrap_content"
android:layout_width="wrap"
android:text="Some text"
/>
<!-- other tags -->
</RelativeLayout>
or in a layout which gets used as list item layout/my_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout...>
<TextView
android:id="@+id/subject"
android:background="@drawable/my_drawable"
android:layout_height="wrap_content"
android:layout_width="wrap"
/>
<!-- other tags -->
</RelativeLayout>
Now, when I alter the color of the shape programmatically inside my ArrayAdapter
of my list, the background of the TextView
inside a fragment at a totally different place in my app gets the same color like the last item in my list! From that time on the shape with the altered color gets used in all other activities which have my_fragment.xml in their layout.
holder.subject = (TextView) v.findViewById(R.id.subject);
GradientDrawable bgShape = (GradientDrawable)holder.subject.getBackground();
bgShape.setColor( position % 2 == 0 ? Color.BLACK : Color.WHITE );
How is that possible and how can I avoid that "reuse" behavior?
Here is the code of my array adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder; // to reference the child views for later actions
if (v == null) {
LayoutInflater vi =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.my_list_item, null);
// cache view fields into the holder
holder = new ViewHolder();
holder.subject = (TextView) v.findViewById(R.id.subject);
// associate the holder with the view for later lookup
v.setTag(holder);
}
else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) v.getTag();
}
GradientDrawable bgShape = (GradientDrawable)holder.subject.getBackground();
bgShape.setColor( position % 2 == 0 ? Color.BLACK : Color.WHITE );
holder.subject.setText(getItem(position));
}
// somewhere else in your class definition
static class ViewHolder {
TextView subject;
}
Upvotes: 0
Views: 458
Reputation: 17671
I have to use bgShape.mutate().setColor(..)
Also see
Upvotes: 0