0

我想在单击列表项时更改它的背景,但它不起作用。

这是我的 MusicAdapter 内部类:

class MusicAdapter extends ArrayAdapter<String>{
        Fill fill=null;
        public MusicAdapter(){
            super(GetMusics.this,android.R.layout.simple_list_item_1,song_list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            row = convertView;
            if(row==null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.row, null);
                fill=new Fill(row);
                row.setTag(fill);
            }else{
                fill=(Fill)row.getTag();
            }
            row.setBackgroundResource(R.drawable.list_selector);
            fill.setRow(song_list.get(position));
            return row;
        }
    }

我也试过这个,但还是不行:

private AdapterView.OnItemClickListener onItemClick = new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        lv.getChildAt(arg2).setBackgroundResource(R.drawable.list_selector);
    }
};

这是我的 list_selector.xml:

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

你有什么建议吗?

4

2 回答 2

0

你必须使用一个叫做Selector并将其设置为的东西ListSelector,请参考这个问题Android ListView Selector Color

于 2012-12-20T22:47:21.683 回答
0

onItemClick()传递您需要更改背景的视图。只需在那里设置背景。

@Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
        long arg3) {
    v.setBackgroundResource(R.drawable.list_selector);
}
于 2012-12-20T21:16:49.963 回答