好吧,我絕對堅持這一點。我有一個ListView模式singleChoice,當我點擊它,我將它設置爲選中。我用選擇器將背景設置爲drawable(我嘗試了許多可以對應於選定項目的狀態)。但是,當我選擇它的背景不會改變,我不明白爲什麼。我爲此訪問了數十個論壇,但無法得到答案。這裏是我的代碼:Android ListView選擇背景
在我的活動ListView控件:
<com.gaetanl.aspa.ui.component.ExtensibleListView
android:id="@+id/payment_billing_address_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_selectable"
android:choiceMode="singleChoice"></com.gaetanl.aspa.ui.component.ExtensibleListView>
其類:
public class ExtensibleListView extends ListView {
public ExtensibleListView(Context context) {
this(context, null);
}
public ExtensibleListView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.listViewStyle);
}
public ExtensibleListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(this, "item " + position + " selected = " + view.isSelected());
view.setSelected(true);
Log.d(this, "selected = " + view.isSelected());
}
});
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
的選擇:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:drawable="@color/colorPrimary" />
<item
android:state_active="true"
android:drawable="@color/colorPrimary" />
<item
android:state_checked="true"
android:drawable="@color/colorPrimary" />
<item
android:state_selected="true"
android:drawable="@color/colorPrimary" />
<item
android:drawable="@android:color/transparent" />
</selector>
我不太清楚你想達到什麼..你想改變一個已經選擇列表視圖項的背景顏色? –
是的就是這樣! –
請發佈您的適配器類的列表視圖請,我可能會然後你。 –