Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 高亮显示ListView所选行_Android_Listview_Row_Highlight - Fatal编程技术网

Android 高亮显示ListView所选行

Android 高亮显示ListView所选行,android,listview,row,highlight,Android,Listview,Row,Highlight,我有一张专辑列表(几百张)。 当我触摸所选唱片集时,我想让用户可以选择播放整个唱片集,或者移动到其曲目列表视图。没问题。 但是,在albumListView中触摸ablum后,我希望该行保持高亮显示,以便用户知道他们单击了哪个项目,然后可以移动到OK或PLAY。如何突出显示ListView中的行?引自 设想一个简单的应用程序,例如ApiDemos,它显示一个文本项列表。用户可以使用轨迹球在列表中自由导航,也可以使用触摸屏滚动和弹出列表。此场景中的问题是,当用户通过触摸屏操作列表时,如何正确处理选

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

引自

设想一个简单的应用程序,例如ApiDemos,它显示一个文本项列表。用户可以使用轨迹球在列表中自由导航,也可以使用触摸屏滚动和弹出列表。此场景中的问题是,当用户通过触摸屏操作列表时,如何正确处理选择

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

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

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


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

<ListView
    android:choiceMode="singleChoice"
.../>
正如默认的
android.R.layout.simple\u list\u item\u single\u choice
使用
RadioButton
表示所选的选项一样,您可以在列表项布局中实现自定义高亮显示状态或类似状态,以响应选项更改。如有必要,应用程序可以使用
getCheckedItemPosition()
等方法来确定用户当前选择的项目


希望有帮助

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


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

我通过扩展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;
}  
我的博客文章中有一个示例程序: 其他解决方案(主要是XML)

1) 将
ListView
choiceMode
设置为
singleChoice

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

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

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

  • ArrayAdapter拥有所选项目的关键字段列表,并提供管理器方法addSelection(MyEntity)和clearAllSelections()。重写的方法getView()读取键字段列表,如果实际位置在其中,则将特定的TextView颜色更改为灰色,否则更改为透明

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


  • 添加
    android:background=“?activatedBackgroundIndicator

    到项目的主布局,类似于
    android.R.layout.simple\u list\u item\u activated\u 2.xml
    以下是我所做的:)-在头部撞墙后

    对于适配器:

     radioAdapter = new RadioAdapter(context, R.layout.row_layout, yourData);
    
    对于第_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
            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>
    
    
    
    将列表\行的选择器写入为

    
    
    确保您已激活状态_的项=true

    然后将此选择器添加为列表行背景

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

    我也遇到了同样的问题,我通过在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>
    

    在我的例子中,当使用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()。

    我知道该问题上次被激活已有两年了,但以防将来有人需要它。以下是我的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的值。

    在这种情况下,我可以使用onClick添加高亮显示吗?onClick Listener将是一种解决方案,或者您可以尝试adamp的方法,但是,在设置中,在gmail中,…,他们实现了所选项目保持高亮显示的列表(当然更方便)他们的论点当然没有被仔细考虑过,因为他们声称,即使在
    <?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" />
    
    android:choiceMode="singleChoice"
    android:listSelector="#003366"
    
    <?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>
    
    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
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/results"
            android:choiceMode="singleChoice"
            android:listSelector="#ffdb700b"/>