列表选择器在android中不工作

列表选择器在android中不工作,android,xml,android-listview,selector,Android,Xml,Android Listview,Selector,我有一个列表视图,我正在其中设置备用背景色。现在我想在单击列表项时使用列表选择器。 这就是我使用游标适配器的getView()设置备用背景的方式 游标适配器 list_selector.xml list_item_bg_pressed.xml 列表\u项\u bg\u normal.xml 最后是我的列表视图,我在其中使用了listSelector activity.xml 我无法解决这个问题。我不知道我哪里出错了。 我的屏幕输出是这样的 尝试以下方式,而不是可绘制: 最后,我找

我有一个列表视图,我正在其中设置备用背景色。现在我想在单击列表项时使用列表选择器。 这就是我使用游标适配器的getView()设置备用背景的方式

游标适配器

list_selector.xml


list_item_bg_pressed.xml


列表\u项\u bg\u normal.xml


最后是我的列表视图,我在其中使用了listSelector

activity.xml


我无法解决这个问题。我不知道我哪里出错了。
我的屏幕输出是这样的

尝试以下方式,而不是可绘制:

最后,我找到了答案。我创建了两个选择器XML文件以提供备用背景。看这个

选择器1


我引用了此

请指定您面临的问题。listSelector不起作用。检查此项:我也尝试了此项。它不工作。不,您不能在drawable中使用颜色。看,我犯了这个错误<代码>不允许的颜色类型(在“可绘制”处,值为“#00000000”)。
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;

} 
<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>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FF0000" />
</shape>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">
     <solid android:color="#00000000" /> <!-- transparent --> 
 </shape>
 <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"/>
Try like this instead of drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="#00000000" android:state_activated="false"/>
    <item android:drawable="#FF0000" android:state_pressed="true"/>
    <item android:drawable="#FF0000" android:state_activated="true"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:state_pressed="false"  android:drawable="@color/black1" />
    <item android:state_pressed="true" android:drawable="@android:color/holo_red_dark" />
    <item android:state_selected="true" android:state_pressed="false"  android:drawable="@android:color/holo_red_dark" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:state_pressed="false"  android:drawable="@color/black2" />
    <item android:state_pressed="true" android:drawable="@android:color/holo_red_dark" />
    <item android:state_selected="true" android:state_pressed="false"  android:drawable="@android:color/holo_red_dark" />
</selector>
if (position % 2 == 0) {
    convertView.setBackgroundResource(R.drawable.selector_one);
 } else {
    convertView.setBackgroundResource(R.drawable.selector_two);
 }