私は 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>
TextView
アプリのいくつかの場所でいくつかの の背景として使用します。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>
または、リスト アイテムとして使用されるレイアウトで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>
ここで、リスト内の形状の色をプログラムで変更すると、アプリ内のまったく別の場所にあるフラグメント内ArrayAdapter
の背景が、リストの最後の項目と同じ色になります! それ以降、色が変更された形状は、レイアウトに my_fragment.xml を持つ他のすべてのアクティビティで使用されます。TextView
holder.subject = (TextView) v.findViewById(R.id.subject);
GradientDrawable bgShape = (GradientDrawable)holder.subject.getBackground();
bgShape.setColor( position % 2 == 0 ? Color.BLACK : Color.WHITE );
どうすればそれが可能になり、その「再利用」動作を回避するにはどうすればよいですか?
これが私のアレイアダプターのコードです:
@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;
}