突出显示ListView选定的行

时间:2011-02-20 16:23:40

标签: android listview row highlight

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

12 个答案:

答案 0 :(得分:42)

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

1)将choiceMode的{​​{1}}设置为ListView

singleChoice

2)列表中的项目必须是Checkable视图,并使用Color State List作为背景

例如:album_item.xml

<ListView
    android:choiceMode="singleChoice"
.../>

3)背景颜色状态(<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/list_selector" .../> )定义突出显示颜色

list_selector.xml

答案 1 :(得分:35)

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

  

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

     

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

     

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

     

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

答案 2 :(得分:19)

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

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

答案 3 :(得分: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;
}  

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

答案 4 :(得分:11)

这是我的解决方案 列表视图:

<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>

将list_row的选择器写为

<!-- <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"/>

确保你有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" />

答案 5 :(得分:6)

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

答案 6 :(得分:5)

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

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

希望有帮助!

答案 7 :(得分:4)

我知道这个问题最后一次活动已经过去了两年,但以防万一其他人需要它。这是我的ListView xml代码

<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/results"
        android:choiceMode="singleChoice"
        android:listSelector="#ffdb700b"/>

您想要更改android:listSelector值。

答案 8 :(得分:3)

以下解决方案可用于突出显示一个或多个列表项。起点:有一个ListFragment通过ArrayAdapter实现显示MyEntity列表。如果选择了项目,则作为项目布局一部分的TextView的颜色必须更改为灰色。应该可以选择多个项目。

  1. ListFragment必须提供将在onListItemClicked()中调用的OnItemSelectListener(YourEntity项)。

    public void onListItemClick(ListView l, View v, int position, long id) {
      MyEntity entity = (MyEntity) getListView().getItemAtPosition(position);
      if (entityListSelectListener != null) 
        itemSelectListener.onItemSelect(entity); }
    
  2. ArrayAdapter拥有所选项的关键字段的Lis​​t,并提供管理器方法addSelection(MyEntity)和clearAllSelections()。覆盖方法getView()读取键字段列表,如果实际位置在那里,则将特定TextView颜色更改为灰色,否则更改为透明。

    public View getView(int position, View convertView, ViewGroup parent) {
      ...
      if (selectedEntityList.contains(entity.key)) //... highlight view field }
    
  3. 在拥有活动的onCreate()中,必须实现侦听器来管理ArrayAdapter:调用addSelection()和notifyDataSetChanged()。

    entityListFragment.setEntityListSelectListener(
      new EntityListFragment.OnItemSelectedListener() {
        public void onItemSelect(MyEntity entity) {
        aa.addSelection(entity);
        aa.notifyDataSetChanged(); }});
    

答案 9 :(得分:2)

我有同样的问题,我通过粘贴ListView代码中的以下代码来解决它

android:choiceMode="singleChoice"
android:listSelector="#003366"

xml文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
    android:id="@+id/file_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
    android:listSelector="#003366"/>

<Button
    android:id="@+id/select"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="SELECT"
    android:layout_below="@id/file_list"/>

</RelativeLayout>

答案 10 :(得分:1)

在我的例子中,当使用simple_list_item_2时,唯一的解决方案是覆盖适配器的getView()方法,只需手动更改背景颜色:

listview = new SimpleAdapter(ctx, data,
                android.R.layout.simple_list_item_2, res,
                new String[] {"field1", "field2" },
                new int[] {android.R.id.text1, android.R.id.text2 })

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);

                if (this_is_the_selected_item)
                {
                    view.setBackgroundColor(0xff999999);
                }
                else
                {
                    view.setBackgroundColor(Color.BLACK);
                }
                return view;
            }
        };

每当您更改所选项目时,请记得调用listview.invalidateViews()。

答案 11 :(得分:0)

这就是我所做的:) - 主要的头撞墙后。

适用于适配器:

 radioAdapter = new RadioAdapter(context, R.layout.row_layout, yourData);

对于row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/radio_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:background="?android:attr/activatedBackgroundIndicator"
android:clickable="true"
android:text="SOME RADIO"
android:textColor="@color/Gray"
android:textSize="25dp" />

要设置要突出显示的默认位置,只需对listView对象执行此操作:

radioTitleList.setItemChecked(defaultPosition,true);

如果您想在点击列表时突出显示:

在自定义列表适配器的getView()中,在返回视图之前添加它:

 row.setOnClickListener(new OnClickListener() 
 {
 @Override
 public void onClick(View arg0) 
 {
     radioTitleList.setItemChecked(position, true);
             // other stuff like navigating to a new activity etc
     }
 });