简体   繁体   English

如何在ListView中更改自定义项目?

[英]How do I change custom items in ListView?

I make custom item for ListView: 我为ListView制作自定义项目:

adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, constants);
lv.setAdapter(adapter);

list_item.xml : 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>

How change background color of LinearLayout in item programmatically? 如何以编程方式更改项目中LinearLayout的背景颜色?

I tried this: 我尝试了这个:

for (int i=0; i<constants.size(); i++){
    lv.getChildAt(i).setBackgroundColor(Color.argb(255, 255, 0, 0));
}

But it is not working. 但这是行不通的。

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;
        }
    }

use as 用于

adapter.setColor(Color.argb(255, 255, 0, 0));
adapter.notifyDataSetChanged();

return defaul 退货

 adapter.setColor(-1);
    adapter.notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM