ListView item background via custom selector

前端 未结 10 1430
深忆病人
深忆病人 2020-11-22 08:19

Is it possible to apply a custom background to each Listview item via the list selector?

The default selector specifies @android:color/transparent for t

相关标签:
10条回答
  • 2020-11-22 08:54

    The article "Why is my list black? An Android optimization" in the Android Developers Blog has a thorough explanation of why the list background turns black when scrolling. Simple answer: set cacheColorHint on your list to transparent (#00000000).

    0 讨论(0)
  • 2020-11-22 08:56

    Never ever use a "background color" for your listview rows...

    this will block every selector action (was my problem!)

    good luck!

    0 讨论(0)
  • 2020-11-22 08:59

    I've been frustrated by this myself and finally solved it. As Romain Guy hinted to, there's another state, "android:state_selected", that you must use. Use a state drawable for the background of your list item, and use a different state drawable for listSelector of your list:

    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>
    

    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>
    

    layout.xml that includes the ListView:

    ...
    <ListView 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:listSelector="@drawable/listitem_selector"
       />
    ...
    

    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>
    
    0 讨论(0)
  • 2020-11-22 09:10

    You can write a theme:

    <pre>
    
        android:name=".List10" android:theme="@style/Theme"
    

    theme.xml

    <style name="Theme" parent="android:Theme">
            <item name="android:listViewStyle">@style/MyListView</item>
    </style>
    

    styles.xml

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

    my_selector is your want to custom selector I am sorry i donot know how to write my code

    0 讨论(0)
  • 2020-11-22 09:11

    instead of:

    android:drawable="@color/transparent" 
    

    write

    android:drawable="@android:color/transparent"
    
    0 讨论(0)
  • 2020-11-22 09:13

    The solution by dglmtn doesn't work when you have a 9-patch drawable with padding as background. Strange things happen, I don't even want to talk about it, if you have such a problem, you know them.

    Now, If you want to have a listview with different states and 9-patch drawables (it would work with any drawables and colors, I think) you have to do 2 things:

    1. Set the selector for the items in the list.
    2. Get rid of the default selector for the list.

    What you should do is first set the 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>
    

    Don't forget the android:state_selected. It works like android:state_focused for the list, but it's applied for the list item.

    Now apply the selector to the items (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>
    

    Make a transparent selector for the list:

    <ListView
        android:id="@+id/android:list"
        ...
        android:listSelector="@android:color/transparent"
        />
    

    This should do the thing.

    0 讨论(0)
提交回复
热议问题