我想單擊時更改列表項目的背景,但它不起作用。點擊時我無法更改列表項目的背景
這是我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>
你有什麼建議嗎?
做用戶點擊一次後永久保存嗎?你的'list_selector.xml'做前者。如果你想做後者,你必須用普通的drawable而不是選擇器來調用'setBackgroundResource()'。 – CloudyMusic