如何解决创建自定义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。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。