突出显示 ListView 选定的行

发布于 2024-10-18 10:02:46 字数 164 浏览 3 评论 0原文

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

I have a list of albums (several hundred).
When I touch the selected album I want to offer the user a choice of playing the whole album, or moving to its track ListView. No problem.
However, after touching the ablum in albumListView I want the row to remain highlighted so the user knows which item they have clicked on, and can then move to OK or PLAY. How do I highlight a row in ListView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

土豪 2024-10-25 10:02:46

其他解决方案(主要在 XML 中)

1) 将 ListViewchoiceMode 设置为 singleChoice

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

2) 列表项必须是 Checkable 查看和使用颜色状态列表 作为背景

例如: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"
.../>

3) 背景颜色状态 ( 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>

Other solution (mostly in XML)

1) set the choiceMode of the ListView to singleChoice

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

2) Items of the list must be a Checkable View and use a Color State List as Background

eg: 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"
.../>

3) the background Color State (list_selector.xml) defines the highlight color

<?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>
水染的天色ゝ 2024-10-25 10:02:46

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

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

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

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

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

Quoted from http://android-developers.blogspot.de/2008/12/touch-mode.html

Imagine a simple application, ApiDemos for example, that shows a list of text items. The user can freely navigate through the list using the trackball but also, alternatively, scroll and fling the list using the touch screen. The issue in this scenario is how to handle the selection properly when the user manipulates the list through the touch screen.

In this case, if the user selects an item at the top of the list and then flings the list towards the bottom, what should happen to the selection? Should it remain on the item and scroll off the screen? What should happen if the user then decided to move the selection with the trackball? Or worse, what should happen if the user presses the trackball to act upon the currently selected item, which is not shown on screen anymore?

After careful consideration, we decided to remove the selection altogether, when the user manipulates the UI through the touch screen.

In touch mode, there is no focus and no selection. Any selected item in a list of in a grid becomes unselected as soon as the user enters touch mode. Similarly, any focused widgets become unfocused when the user enters touch mode. The image below illustrates what happens when the user touches a list after selecting an item with the trackball.

诗化ㄋ丶相逢 2024-10-25 10:02:46

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

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

Maaalte is correct, in touch mode lists do not show the focus highlight on a current item since there is no current item in touch mode. Stateful selection like you're describing that persists while you interact with other parts of the UI is another matter though.

You can use CHOICE_MODE_SINGLE to express a stateful selection in a list. It will treat selection as a checked state. As long as the view that your adapter returns implements the Checkable interface, you can display the selected state however you choose. (Setting a background on the item view or otherwise. It does not actually need to show a check box widget.) You can then use the item checked methods of ListView to manipulate or determine the selection.

分分钟 2024-10-25 10:02:46

我通过扩展 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 -选择工作/

I solved this by extending the ListView and overriding the getView() method. I kept an internal identifier for the "selected" state and changed the background of the item according to it, like this:

@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;
}  

I have an example program on my blog post:
http://udinic.wordpress.com/2011/07/01/selectablelistview-make-selection-work/

羁绊已千年 2024-10-25 10:02:46

这是我的解决方案
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>

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

Here is my solution
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>

Write a selector for 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"/>

Make sure you have item for state_activated=true

Then add this selector as your list_row background

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

半寸时光 2024-10-25 10:02:46

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

add android:background="?activatedBackgroundIndicator
to list item's main layout similar to android.R.layout.simple_list_item_activated_2.xml

杀お生予夺 2024-10-25 10:02:46

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

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

希望有帮助!

Implement your list with single choice mode: android:choiceMode=singleChoice

Just as the default android.R.layout.simple_list_item_single_choice uses a RadioButton to denote the selected choice, you can implement a custom highlight state or similar in your list item layout to respond to the choice changes. Methods like getCheckedItemPosition() can be used by your application to determine which item the user currently has chosen, if necessary.

Hope that Helps!

回心转意 2024-10-25 10:02:46

我知道这个问题上次活跃已经有两年了,但以防万一其他人将来需要它。这是我的 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 值。

I know that it's two years since this question was last active, but just in case someone else needs it in the future. Here's my ListView xml code

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

You want to change the android:listSelector value.

水溶 2024-10-25 10:02:46

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

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

    public void onListItemClick(ListView l, View v, int 位置, 长 id) {
      MyEntity 实体 = (MyEntity) getListView().getItemAtPosition(position);
      if (entityListSelectListener != null) 
        itemSelectListener.onItemSelect(实体); }
    
  2. ArrayAdapter 拥有一个包含所选项目的关键字段的列表,并提供管理器方法 addSelection(MyEntity) 和clearAllSelections()。重写方法 getView() 读取关键字段列表,如果实际位置在其中,则将特定 TextView 颜色更改为灰色,否则更改为透明。

    public View getView(intposition,ViewconvertView,ViewGroupparent){
      ...
      if (selectedEntityList.contains(entity.key)) //...突出显示视图字段 }
    
  3. 在拥有活动的 onCreate() 中,必须实现监听器来管理 ArrayAdapter:调用 addSelection() 和 notificationDataSetChanged()。

    entityListFragment.setEntityListSelectListener(
      新的 EntityListFragment.OnItemSelectedListener() {
        公共无效onItemSelect(MyEntity实体){
        aa.addSelection(实体);
        aa.notifyDataSetChanged(); }});
    

Following solution can be used for highlighting one ore many list items. Starting point: There is a ListFragment to show a List of MyEntity via an ArrayAdapter implementation. The color of an TextView as part of the item layout has to change to grey, if the item is selected. Multiple items should be selectable.

  1. The ListFragment must provide an OnItemSelectListener(YourEntity item) that will be called in onListItemClicked().

    public void onListItemClick(ListView l, View v, int position, long id) {
      MyEntity entity = (MyEntity) getListView().getItemAtPosition(position);
      if (entityListSelectListener != null) 
        itemSelectListener.onItemSelect(entity); }
    
  2. The ArrayAdapter owns a List for the key fields for the selected items and provides the manager methods addSelection(MyEntity) and clearAllSelections(). The overriden method getView() reads the key field list and, if the actual position is in there, changes the particular TextView color to grey, otherwise to transparent.

    public View getView(int position, View convertView, ViewGroup parent) {
      ...
      if (selectedEntityList.contains(entity.key)) //... highlight view field }
    
  3. In onCreate() of the owning activity the listeners has to be implemented to manage the ArrayAdapter: calling addSelection() and notifyDataSetChanged().

    entityListFragment.setEntityListSelectListener(
      new EntityListFragment.OnItemSelectedListener() {
        public void onItemSelect(MyEntity entity) {
        aa.addSelection(entity);
        aa.notifyDataSetChanged(); }});
    
别想她 2024-10-25 10:02:46

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

I had the same problem, I solved it by pasting the following code in the ListView code

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

The xml file looks as the following

<?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>
把回忆走一遍 2024-10-25 10:02:46

就我而言,当使用 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() 。

In my case, when using simple_list_item_2, the only solution was to override the getView() method of the adapter, and just change the background color manually:

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

Remember to call listview.invalidateViews() whenever you change the selected item.

网白 2024-10-25 10:02:46

这就是我所做的:) - 在头撞墙之后。

对于适配器:

 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
     }
 });

Here's what I did :) - after major head whacking against wall.

For the adapter:

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

For the 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" />

To set a default position to be highlighted just do this to your listView object:

radioTitleList.setItemChecked(defaultPosition, true);

If you want highlighting when you click on the list:

In your getView() in your custom list adapter add this before returning the view:

 row.setOnClickListener(new OnClickListener() 
 {
 @Override
 public void onClick(View arg0) 
 {
     radioTitleList.setItemChecked(position, true);
             // other stuff like navigating to a new activity etc
     }
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文