Android的ListView控件将不突出所选行 [英] android ListView won't highlight selected row
问题描述
在我的应用我获取用户从服务器播放列表,并觉得的ListView
的数据。问题是,是,的ListView
并不突出单击行。为prevent愤怒的评论告诉我,我这里不使用搜索的事情,我已经试过:
In my app I retrieve a user's playlist from the server and feel a ListView
with the data. The problem is, that the ListView
does not highlight clicked rows. To prevent rage comments telling me that I don't use search here are things I've tried:
- 在我行设定按钮不可聚焦
- 将code和XML(而不是在同一时间)设置listSelector两个
- 在试图改变在通过
查看
背景的OnItemClickListener
- 在使用setItemsCanFocus()与僵尸真假参数 li>
- 设置的XML绘制与国家选择和pressed排布局根
- setting the button in my row to non-focusable
- setting the listSelector both by code and XML (not at the same time)
- trying to change the background of passed
View
in anOnItemClickListener
- using setItemsCanFocus() with bot true and false parameters
- setting an XML drawable with states "selected" and "pressed" to row layout root
点击检测到,因为我测试了使用吐司
。但我就是不能强迫的ListView
高亮所选行
Clicks are detected since I tested that using a Toast
. But I just can't force the ListView
to highlight the selected row
ListView控件
ListView
<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>
作为列表行布局
Layout used as list row
<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>
有什么建议?
推荐答案
创建一个选择器,在你的可绘制文件夹列表视图,并设置列表项的背景这一绘制。请参阅下面
Create a selector for the list view in your drawables folder and set the list item background to this drawable. See the code below
select_button_background.xml
select_button_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>
select_button_selectable.xml
select_button_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>
select_button_selected.xml
select_button_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的ListView控件将不突出所选行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!