我有列表視圖,我在其中設置替代背景顏色。現在我想在列表項目單擊時使用列表選擇器。 這是我設置使用光標適配器列表選擇器在android中不工作
的CursorAdapter
public View getView(int position, View convertView, ViewGroup parent) {
final View row = super.getView(position, convertView, parent);
if (position % 2 == 0)
row.setBackgroundColor(Color.parseColor("#191919"));
else
row.setBackgroundColor(Color.parseColor("#323232"));
return row;
}
list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
list_item_的getView()替代背景的方式bg_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
list_item_bg_normal.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00000000" /> <!-- transparent -->
</shape>
,最後我有我都用我的listSelector列表視圖。
activity.xml
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myListView"
android:listSelector="@drawable/list_selector"
android:drawSelectorOnTop="true"
android:clickable="true"/>
我無法解決這個問題。我不知道我錯在哪裏。我的屏幕
輸出是這樣
你面對請註明什麼問題。 –
listSelector不起作用。 –
檢查此:http://stackoverflow.com/questions/18386035/custom-selector-for-list-background –