Android ListSelector 适用于整个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:52:03  来源:igfitidea点击:

ListSelector applies to the entire list

android

提问by jax

I have a simple list with a listselector like so.

我有一个像这样的列表选择器的简单列表。

<ListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_below="@+id/round"
    android:listSelector="#99000000" android:clickable="true" android:cacheColorHint="#00000000" android:background="#00000000">
</ListView>

As you can see android:listSelector="#99000000" but the "black alpha" color is applied to the entire list, not the selected item.

正如您所看到的 android:listSelector="#99000000" 但“黑色 alpha”颜色应用于整个列表,而不是所选项目。



So this is what I have now but the entire list still turns black

所以这就是我现在拥有的但整个列表仍然变黑

::listview_background.xml

::listview_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/list_normal" />
  <item android:state_pressed="true"
        android:drawable="@drawable/list_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/list_active" />
</selector>

::colors.xml

::颜色.xml

<resources>
    <drawable name="list_normal">#96FFFFFF</drawable>
    <drawable name="list_active">#66000000</drawable>
    <drawable name="list_pressed">#CA000000</drawable>
</resources>

::the xml tag in my list

::我列表中的 xml 标签

android:listSelector="@drawable/listview_background"

采纳答案by Eric

I had the same problem. I have a custom background image, and I don't want to have to make variants of that background image because that would be tedious to represent all the different states.

我有同样的问题。我有一个自定义背景图像,我不想制作该背景图像的变体,因为代表所有不同的状态会很乏味。

So I want to do the obvious thing, have a semi-transparent bar that is overlayed on top of the focused listitem and when the user taps the "enter" key or whatever, it flashes to the pressed overlay color which is more striking and somewhat more opaque.

所以我想做一个显而易见的事情,有一个半透明的栏覆盖在焦点列表项的顶部,当用户点击“输入”键或其他任何东西时,它会闪烁到更醒目的叠加颜色更不透明。

The solution was to stay away from any @color or @drawable that refers to a color inside listSelector. I created two 3x3 pixel .png files. Each saved with the gamma layer. In my case it's two of the same color each mixed down in Gimp with a different transparency on the color layer. So when you select an item you get an overlay with 25% color, and when you press it you get a png with 50% color. I put them in my drawables as bg_list_item_pressed.png and bg_list_item_highlighted.png

解决方案是远离任何引用 listSelector 中颜色的 @color 或 @drawable。我创建了两个 3x3 像素的 .png 文件。每个都与伽马层一起保存。在我的例子中,它是两个相同颜色的,每个在 Gimp 中混合在一起,颜色层上的透明度不同。所以当你选择一个项目时,你会得到一个 25% 颜色的叠加层,当你按下它时,你会得到一个 50% 颜色的 png。我将它们作为 bg_list_item_pressed.png 和 bg_list_item_highlighted.png 放在我的 drawable 中

Then I set my list selector to:

然后我将我的列表选择器设置为:

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

  <!-- Selected --> 
  <item 
    android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/bg_list_item_highlighted" /> <!--  @drawable/tab_focus -->

  <!-- Pressed -->
  <item 
    android:state_pressed="true" 
    android:drawable="@drawable/bg_list_item_pressed" /> <!--  @drawable/tab_press -->

</selector> 

Then I added my listSelector attributes to my ListView in my layout xml:

然后我在我的布局 xml 中将我的 listSelector 属性添加到我的 ListView 中:

android:listSelector="@drawable/list_selector"
android:drawSelectorOnTop="true"

Now it works exactly how I want it to work. Including using the D-pad to select a row, and click it with enter. Getting the highlighting and subsequent pressing colors exactly how they should be.

现在它完全按照我想要的方式工作。包括使用方向键选择一行,然后用回车键单击它。准确地获得突出显示和随后的压制颜色。

回答by Pilot_51

I was having this same issue and while looking at one of the platform drawable XML files I noticed a way to eliminate the need to create an image file just for a color, by creating a shape in XML.

我遇到了同样的问题,在查看一个平台可绘制 XML 文件时,我注意到一种通过在 XML 中创建形状来消除仅为颜色创建图像文件的需要的方法。

For example, instead of:

例如,而不是:

<item android:state_focused="true"
      android:drawable="@drawable/list_active" />

Do:

做:

<item android:state_focused="true">
    <shape>
        <solid android:color="#66000000" />
    </shape>
</item>

Beyond just creating a plain color drawable, the shape is flexible, similar to a simple vector object. All the details can be found here: http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

除了创建纯色可绘制之外,形状还很灵活,类似于简单的矢量对象。所有细节都可以在这里找到:http: //developer.android.com/guide/topics/resources/drawable-resource.html#Shape

回答by worked

If you are setting up a row layout for each item in the list, this is how I do it. Note the transparent value set to the listSelector. (For brevity, I removed the colors.xml.)

如果您要为列表中的每个项目设置行布局,我就是这样做的。注意为 listSelector 设置的透明值。(为简洁起见,我删除了colors.xml。)

main.xml

主文件

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:listSelector="#00000000"/>
</LinearLayout>

row.xml

行.xml

<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/row"
  android:layout_width="fill_parent"
  android:layout_height="?android:attr/listPreferredItemHeight"
  android:background="@drawable/list_selector">
  ...
</FrameLayout>

list_selector.xml

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="#66FFFFFF" />
    <item android:drawable="#FF666666"/> 
</selector>

回答by noobular

Jax,

贾克斯,

I was trying to figure out why my entire list was changing color setting the listSelector, and found out that this is exactly what it is supposed to do, i.e. we are setting the color for the entire list, and not the row which is what we want.

我试图弄清楚为什么我的整个列表正在更改设置 listSelector 的颜色,并发现这正是它应该做的,即我们正在为整个列表设置颜色,而不是我们设置的行想。

Here is what you want to do: I assume you have a ListView defined in XML. And for this ListView, I assume you are using an adapter of some sort to set present the data. Also for this adapter, you are using some row view that is defined in XML. What you want to do is set the background of this row element like this:

这是您想要做的:我假设您有一个用 XML 定义的 ListView。对于这个 ListView,我假设您使用某种适配器来设置呈现数据。同样对于此适配器,您正在使用一些在 XML 中定义的行视图。你想要做的是像这样设置这个行元素的背景:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="@drawable/textview_selector">
        <TextView       android:id="@+id/textForList"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:padding="10sp"  />
</LinearLayout>

Here is my textview_selector.xml stored in drawable:

这是我存储在 drawable 中的 textview_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/teal" />
    <item android:drawable="@drawable/white" />
</selector>

Hope this helps

希望这可以帮助

回答by Christopher Orr

The list selector is a StateListDrawable— it contains reference to multiple drawables for each state the list can be, like selected, focused, pressed, disabled...

列表选择器是一个StateListDrawable- 它包含对列表可以处于的每个状态的多个可绘制对象的引用,例如选择、聚焦、按下、禁用......

In your case, by setting a single colour, the list is drawing #9000 for every single state.

在您的情况下,通过设置单一颜色,列表为每个状态绘制 #9000。

You need to define a StateListDrawableas described above, with XML similar to the following. This is what you set in your android:listSelectorattribute.

您需要StateListDrawable按照上面的描述定义一个,使用类似于下面的 XML。这是您在android:listSelector属性中设置的内容。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:state_focused="true"
        android:drawable="@drawable/item_disabled" />
  <item android:state_pressed="true"
        android:drawable="@drawable/item_pressed" />
  <item android:state_focused="true"
        android:drawable="@drawable/item_focused" />
</selector>

Or, to set the same list selector for all lists in your application, you can override the selector in the Widget.ListViewtheme:

或者,要为应用程序中的所有列表设置相同的列表选择器,您可以覆盖Widget.ListView主题中的选择器:

<style name="Widget.ListView" parent="Widget.AbsListView">
  <item name="android:listSelector">@drawable/my_custom_selector</item>
</style>

回答by ferry

You don't always need to create an image, you can define a shape

你并不总是需要创建一个图像,你可以定义一个形状

<?xml version="1.0" encoding="utf-8"?>

<item android:state_focused="true" android:state_pressed="false">
     <shape android:shape="rectangle">
         <solid android:color="#40000000"/>
     </shape>
</item>
<item android:state_pressed="true">
     <shape android:shape="rectangle">
         <solid android:color="#80000000"/>
     </shape>
</item>