通过自定义选择器ListView项目背景

shi*_*ira 97 android listview

是否可以通过列表选择器将自定义背景应用于每个Listview项?

默认选择器指定@android:color/transparentstate_focused="false"大小写,但将其更改为某个自定义drawable不会影响未选中的项目.Romain Guy似乎在这个答案中建议这是可能的.

我目前正在通过在每个视图上使用自定义背景并在选择/聚焦项目时隐藏它来实现相同的效果,以便显示选择器,但是在一个地方将所有这些都定义为更优雅.

作为参考,这是我用来试图让它工作的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="false"
        android:drawable="@drawable/list_item_gradient" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@drawable/list_selector_background_disabled" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/list_selector_background_transition" />

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

</selector>
Run Code Online (Sandbox Code Playgroud)

这就是我设置选择器的方式:

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/list_selector_background" />    
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助!

dgm*_*ltn 128

我自己一直很沮丧,最后解决了.正如Romain Guy暗示的那样,还有另一个州,"android:state_selected"你必须使用它.使用状态drawable作为列表项的背景,并使用不同的状态drawable为listSelector您的列表:

list_row_layout.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="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listitem_background"
    >
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

listitem_background.xml:

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

包含ListView的layout.xml:

...
<ListView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:listSelector="@drawable/listitem_selector"
   />
...
Run Code Online (Sandbox Code Playgroud)

listitem_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/listitem_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/listitem_selected" />
</selector>
Run Code Online (Sandbox Code Playgroud)


Mic*_*ł K 78

当你有一个带有填充作为背景的9补丁drawable时,dglmtn的解决方案不起作用.奇怪的事情发生了,我甚至不想谈论它,如果你有这样的问题,你就知道它们.

现在,如果你想拥有一个具有不同状态的列表视图和9-patch drawables(它适用于任何drawables和颜色,我认为)你必须做两件事:

  1. 为列表中的项目设置选择器.
  2. 摆脱列表的默认选择器.

你应该做的是首先设置row_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_enabled="true" 
     android:state_pressed="true" android:drawable="@drawable/list_item_bg_pressed" />
    <item android:state_enabled="true"
     android:state_focused="true" android:drawable="@drawable/list_item_bg_focused" />
    <item android:state_enabled="true"
     android:state_selected="true" android:drawable="@drawable/list_item_bg_focused" />
    <item
     android:drawable="@drawable/list_item_bg_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)

别忘了android:state_selected.它的工作方式android:state_focused与列表相似,但它适用于列表项.

现在将选择器应用于项目(row.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="wrap_content"
android:orientation="horizontal"
android:background="@drawable/row_selector"
>
...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

为列表创建透明选择器:

<ListView
    android:id="@+id/android:list"
    ...
    android:listSelector="@android:color/transparent"
    />
Run Code Online (Sandbox Code Playgroud)

这应该做的事情.

  • 对不起,但是有些事情让人有时候想到为什么这些该死的UI apis必须如此蹩脚.Android还有很多需要经历的事情.这样的基本事情不应该是这样的PITA. (27认同)
  • 而不是android:listSelector ="@ drawable/list_selector"只需编写android:listSelector ="@ android:color/transparent".不需要额外的list_selector.xml. (3认同)
  • +1谢谢,谢谢,谢谢.跳过所有这些箍,实现一些基本的东西.所有这些状态都非常令人困惑. (2认同)

cV2*_*cV2 17

永远不要为列表视图行使用"背景颜色"......

这将阻止每个选择器操作(是我的问题!)

祝好运!

  • 这不是真的.只需使用选择器作为背景,而不是静态颜色或可绘制 (3认同)

Sco*_*der 5

Android开发者博客中的文章"为什么我的列表是黑色的?Android优化"详细解释了滚动时列表背景变黑的原因.简单回答:将列表中的cacheColorHint设置为透明(#00000000).


Cab*_*zas 5

这就够了,如果你投入list_row_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/listitem_background">... </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

listitem_selector.xml:

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

  • 太棒了......最简单的例子 (2认同)