[英]How do I change custom items in ListView?
我為ListView制作自定義項目:
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, constants);
lv.setAdapter(adapter);
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listitem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4d73ff"
android:orientation="vertical" >
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:minHeight="48dp"
android:padding="10dp"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
如何以編程方式更改項目中LinearLayout的背景顏色?
我嘗試了這個:
for (int i=0; i<constants.size(); i++){
lv.getChildAt(i).setBackgroundColor(Color.argb(255, 255, 0, 0));
}
但這是行不通的。
class MyAdapter extends ArrayAdapter{
private int color;
MyAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
color = -1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rootView = super.getView(position, convertView, parent);
if (color != -1){
rootView.setBackgroundColor(color);
}
return rootView;
}
public void setColor(int color) {
this.color = color;
}
}
用於
adapter.setColor(Color.argb(255, 255, 0, 0));
adapter.notifyDataSetChanged();
退貨
adapter.setColor(-1);
adapter.notifyDataSetChanged();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.