创建自定义ListView选择器时出现问题

问题描述

| 当我尝试单击列表视图项时,没有显示选择器。这是我的ListView
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" android:layout_height=\"wrap_content\" android:layout_width=\"wrap_content\" android:id=\"@+id/streamRelativeLayout\">
    <ListView android:layout_height=\"fill_parent\" android:layout_width=\"fill_parent\" android:id=\"@+id/streamListView\" android:cacheColorHint=\"#00000000\" android:fadingEdge=\"none\" android:listSelector=\"@drawable/swipeview_list_selector\">   </ListView>
    <TextView android:layout_centerInParent=\"true\" android:layout_height=\"wrap_content\" android:id=\"@+id/noStreamTextView\" android:layout_width=\"wrap_content\" android:text=\"No Stream Available\" android:visibility=\"invisible\"></TextView>
    <ProgressBar android:layout_centerInParent=\"true\" android:layout_height=\"wrap_content\" android:id=\"@+id/streamProgressBar\" android:layout_width=\"wrap_content\"></ProgressBar>
</RelativeLayout>
这是我的选择器:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">
  <item android:state_enabled=\"false\" android:state_focused=\"true\"
        android:drawable=\"@drawable/stocks_gradient\" />
  <item android:state_pressed=\"true\"
        android:drawable=\"@drawable/stocks_gradient\" />
  <item android:state_focused=\"true\"
        android:drawable=\"@drawable/stocks_gradient\" />
</selector>
这是渐变:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<shape xmlns:android=\"http://schemas.android.com/apk/res/android\">
    <gradient 
        android:startColor=\"#ECECEC\"
        android:centerColor=\"#F6F6F4\"
        android:endColor=\"#F8F8F6\"
        android:angle=\"90\"
        android:dither=\"true\"
     />
</shape>
我还将背景色设置为在getView()中交替显示
int colorPos = position % colors.length;
            v.setBackgroundColor(colors[colorPos]);
    

解决方法

如果您表示所选项目没有按照您想要的方式显示,那是因为您错过了
state_selected
情况:
<item android:state_selected=\"true\" android:drawable=\"...\"/>
    ,我看到的第一个问题是,所有状态都使用相同的可绘制对象,因此单击该对象不会看到更改。而且您还没有考虑所有情况,例如您没有默认状态。     ,我需要将selectorontop属性设置为true。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...