突出显示ListView选定的行

37 android listview row highlight

我有一张专辑列表(几百张).当我触摸所选专辑时,我想让用户选择播放整个专辑,或者移动到其轨道ListView.没问题.但是,在触摸albumListView中的ablum之后,我希望该行保持突出显示,以便用户知道他们点击了哪个项目,然后可以移动到OK或PLAY.如何在ListView中突出显示一行?

Hie*_*iep 42

其他解决方案(主要是XML)

1)设置choiceModeListViewsingleChoice

<ListView
    android:choiceMode="singleChoice"
.../>
Run Code Online (Sandbox Code Playgroud)

2)清单的项目必须是一个可检查查看和使用颜色列表为背景

例如:album_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@drawable/list_selector"
.../>
Run Code Online (Sandbox Code Playgroud)

3)背景Color State(list_selector.xml)定义高亮颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/green" android:state_checked="true"/>
    <item android:drawable="@color/green" android:state_pressed="true"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

  • 这里最重要的是第1步,其他则不是必需的.对于背景样式,只需在项目中定义:android:background ="?android:attr/activatedBackgroundIndicator" (5认同)
  • 试过这个,这是一个很好的解决方案! (3认同)
  • 你如何让选择颜色留下来?它只是暂时显示颜色然后消失 (2认同)

Maa*_*lte 35

引自http://android-developers.blogspot.de/2008/12/touch-mode.html

想象一下一个简单的应用程序,例如ApiDemos,它显示了一个文本项列表.用户可以使用轨迹球自由地浏览列表,但也可以使用触摸屏滚动和拖动列表.此方案中的问题是当用户通过触摸屏操作列表时如何正确处理选择.

在这种情况下,如果用户选择列表顶部的项目然后将列表向底部移动,那么选择会发生什么?它应该保留在项目上并滚动屏幕吗?如果用户随后决定使用轨迹球移动选择,会发生什么?或者更糟糕的是,如果用户按下轨迹球以对当前所选项目进行操作会发生什么情况,而该屏幕上不会再显示该项目?

仔细考虑之后,当用户通过触摸屏操作UI时,我们决定完全删除选择.

在触摸模式下,没有焦点也没有选择.用户进入触摸模式后,网格列表中的任何选定项目都将被取消选中.类似地,当用户进入触摸模式时,任何聚焦的小部件都变得不聚焦.下图说明了在用轨迹球选择项目后用户触摸列表时会发生什么.

  • 然而,在设置中,在gmail,...中,他们实现了所选项目保持突出显示的列表(当然这更方便).他们的论点当然没有经过仔细考虑,因为他们声称,即使用户将其滚出屏幕,也会突出显示项目. (21认同)

ada*_*amp 19

Maaalte是正确的,在触摸模式下,列表不会在当前项目上显示焦点突出显示,因为在触摸模式下没有当前项目.有状态的选择就像你描述的那样在你与UI的其他部分交互时持续存在是另一回事.

您可以使用CHOICE_MODE_SINGLE在列表中表示有状态选择.它会将选择视为已检查状态.只要适配器返回的视图实现了Checkable接口,您就可以显示所选的状态.(在项目视图或其他方面设置背景.它实际上不需要显示复选框小部件.)然后,您可以使用ListView的项目检查方法来操作或确定选择.


Udi*_*nic 12

我通过扩展ListView并覆盖getView()方法解决了这个问题.我保留了"选定"状态的内部标识符,并根据它更改了项目的背景,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View currView = super.getView(position, convertView, parent);
StateListItem currItem = getItem(position);
if (currItem.isItemSelected) {
    currView.setBackgroundColor(Color.RED);
} else {
    currView.setBackgroundColor(Color.BLACK);
}
return currView;
}  
Run Code Online (Sandbox Code Playgroud)

我的博客文章中有一个示例程序:http: //udinic.wordpress.com/2011/07/01/selectablelistview-make-selection-work/


sub*_*r_a 11

这是我的解决方案Listview:

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:choiceMode="singleChoice"
        android:paddingTop="8dip" >
    </ListView>
Run Code Online (Sandbox Code Playgroud)

为list_row写一个选择器as

<!-- <item android:drawable="@color/android:transparent"  android:state_selected="true" /> -->
<item android:drawable="@android:color/darker_gray" android:state_selected="true"/>
<item android:drawable="@android:color/darker_gray" android:state_activated="true"/>    
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
<item android:drawable="@color/android:transparent"/>
Run Code Online (Sandbox Code Playgroud)

确保你有state_activated = true的项目

然后将此选择器添加为list_row背景

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/listitem_selector"
  android:orientation="vertical" >

<TextView
    android:id="@+id/itemName"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)


小智 6

添加android:background="?activatedBackgroundIndicator
到列表项的主要布局类似于android.R.layout.simple_list_item_activated_2.xml


Dev*_*red 5

使用单一选择模式实现列表: android:choiceMode=singleChoice

正如默认android.R.layout.simple_list_item_single_choice使用a RadioButton来表示所选的选项一样,您可以在列表项布局中实现自定义突出显示状态或类似状态,以响应选择更改.getCheckedItemPosition()您的应用程序可以使用类似的方法来确定用户当前选择的项目(如有必要).

希望有帮助!