android ListView赢得';t突出显示所选行
在我的应用程序中,我从服务器检索到一个用户的播放列表,并感觉到一个带有数据的android ListView赢得';t突出显示所选行,android,android-listview,Android,Android Listview,在我的应用程序中,我从服务器检索到一个用户的播放列表,并感觉到一个带有数据的ListView。问题是,ListView没有突出显示单击的行。为了防止愤怒的评论告诉我不使用搜索,我尝试了以下几点: 将我行中的按钮设置为不可聚焦 通过代码和XML设置listSelector(不能同时设置) 试图在OnItemClickListener中更改传递的视图的背景 将setItemsCanFocus()与bot true和false参数一起使用 将状态为“selected”和“pressed”的XML d
ListView
。问题是,ListView
没有突出显示单击的行。为了防止愤怒的评论告诉我不使用搜索,我尝试了以下几点:
- 将我行中的按钮设置为不可聚焦
- 通过代码和XML设置listSelector(不能同时设置)
- 试图在
OnItemClickListener中更改传递的
视图的背景
- 将setItemsCanFocus()与bot true和false参数一起使用
- 将状态为“selected”和“pressed”的XML drawable设置为行布局根目录
Toast
测试后,检测到点击。但是我无法强制列表视图
突出显示所选行
列表视图
<ListView
android:id="@android:id/list"
android:listSelector="@drawable/list_selected"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/etbg"
android:choiceMode="singleChoice"
android:divider="@color/div" >
</ListView>
用作列表行的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tvListItemArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Small Text"
android:layout_toRightOf="@+id/tbListPlay"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/tvListItemSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tvListItemArtist"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tvListItemArtist"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/img_list_audio_saved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="50dp"
android:src="@drawable/attention"
android:visibility="gone" />
<Button
android:id="@+id/tbListPlay"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/button_play"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
有什么建议吗?您已经更改了ListSelector:
android:ListSelector=“@drawable/list\u selected”
我假设您的自定义drawable不是一个选择器,它具有不同的状态。查看此或此以了解用法在drawables文件夹中为列表视图创建选择器,并将列表项背景设置为此drawables。请参见下面的代码
选择\u按钮\u background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/select_button_selected" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/select_button_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/select_button_selectable" android:state_pressed="false"/>
<item android:drawable="@drawable/select_button_selectable" android:state_selected="false"/>
</selector>
选择\u按钮\u selectable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:endColor="#CCCCCC"
android:startColor="#DDDDDD"
android:type="linear" />
<corners android:radius="10dp" />
<shape>
<stroke
android:width="1dp"
android:color="#333333" />
</shape>
</shape>
选择按钮\u selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:endColor="#AAAAAA"
android:startColor="#BBBBBB"
android:type="linear" />
<corners android:radius="10dp" />
<shape>
<stroke
android:width="1dp"
android:color="#333333" />
</shape>
</shape>
您好,我已经在这个问题链接中为您发布了答案,您说它对您不起作用,我已经提供了相同的答案,它对其他检查此问题有效。我希望这会对您有所帮助。您可以在android文件夹中找到选择器:android sdk\platforms\android-14\data\res\drawable。然后将它复制到您的项目中,并根据您的需要进行更改。我尝试删除listSelector属性,这意味着应该使用默认的属性。没有,我只是抽时间做那个项目。您的解决方案有效,谢谢