3

我正在尝试自定义 aListView以使每一行都具有默认背景图像和突出显示的背景图像。

但是,高亮背景图片影响单行,默认背景图片影响整个ListView;但我需要它来影响每一行。

有人可以告诉我该怎么做吗?

这是我的代码:

布局/main.xml:

<ListView
 android:id="@+id/list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:dividerHeight="1dip"       
 android:listSelector="@drawable/bg_highlighted"
/>

可绘制/选择器.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_selected="true" android:drawable="@drawable/bg_default"/>
<item android:state_focused="true" android:drawable="@drawable/bg_default"/>

</selector>

src/main.java:

   ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,R.layout.list, mStrings); /*"Item1","Item2","Item3","Item4"*/
    ListView lv = (ListView)findViewById(R.id.list);
    lv.setAdapter(adapter);
    lv.setBackgroundResource(R.drawable.selector);

作为默认背景和突出显示的背景,我使用 png 图像。

这就是我所拥有的,但这就是我想要的。

4

3 回答 3

1

I did like heepie advised above.
And as I understand there is not a property that allow to set background drawable to single row of ListView directly. It's should be done by creating custom view in getView method of a custom adapter. This is my final code. May be it will be useful to someone.

layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" >

        <ListView
           android:id="@+id/my_list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
         />

</LinearLayout>

layout/my_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="left|center_vertical"
    android:paddingLeft="6dip"
    android:textColor="#FFFFFF"
    android:background="@drawable/list_selector"    
/>

drawable/list_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/bg_highlighted" />
    <item android:drawable="@drawable/bg_default" />
</selector>

Drawable png images:
drawable/bg_default.png:
bg_default

drawable/bg_highlighted.png:
bg_highlighted

src/main.java:

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyAdapter adapter;
        ArrayList<String> mStrings = new ArrayList<String>();
        mStrings.add("Item 1");
        mStrings.add("Item 2");
        mStrings.add("Item 3");
        mStrings.add("Item 4");

        ListView lv = (ListView) findViewById(R.id.my_list);
        adapter = new MyAdapter(this, mStrings);
        lv.setAdapter(adapter);
    }
}

src/MyAdapter.java:

public class MyAdapter extends BaseAdapter {

public Activity activity;
ArrayList<String> data = new ArrayList<String>();
private static LayoutInflater inflater = null;

public MyAdapter(Activity a, ArrayList<String> d) {
    activity = a;
    data = d;
    inflater = LayoutInflater.from(activity);
}    

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.my_list_item, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Touch on view handle
            Log.d("", "Touched row "+position);
        }

    });

    //customizing view
    holder.textView = (TextView)convertView.findViewById(R.id.my_textview);
    holder.textView.setText(data.get(position));

    return convertView;
}       

public static class ViewHolder {
    public TextView textView;
}
@Override
public int getCount() {
    return data.size();
}
@Override
public Object getItem(int position) {
    return position;
}
@Override
public long getItemId(int position) {
    return position;
}
}

And this is a result:

result.png

于 2012-04-24T23:21:20.170 回答
0

使用扩展 baseadapter 的新类并从 getview 方法更改每一行的背景,而不是使用 arrayadapter 可能会更好

于 2012-04-23T02:18:12.900 回答
0

我看到的问题是您实际上在这里使用项目选择器作为整个 ListView 背景:

lv.setBackgroundResource(R.drawable.selector);

您所要做的就是将此选择器设置为您内部根视图的背景layout/list.xml(为列表中的每个项目膨胀的布局)

于 2012-04-23T00:08:28.280 回答