views:

4176

answers:

5

How can I change background color of ListView items on a pair-item basis. When I use android:backgroundColor in the ListView item layout I can achieve this, however the list selector is no longer visible. I can make the selector visible again by setting drawSelectorOnTop to true but then the selector overlays the whole item.

Any ideas how to change those background colors and keep the selector?

PS I would rather not change the selector itself.

EDIT: Authors of GMail application have managed to achieve exactly this so it's definitely possible.

A: 

Take a look at List14 example. In getView() you can call convertView.setBackgroundDrawable() for each entry. You could have a class member counter to decide which background to call it with to get alternating backgrounds, for example.

Heikki Toivonen
Unfortunately this doesn't work. Setting BackgroundDrawable has the same effect as setting BackgroundColor - the selector is drawn first so it's hidden.
Immortal
Change your `android:drawSelectorOnTop` to be `false`.
CommonsWare
+3  A: 

PS I would rather not change the selector itself.

If you don't want to do that (why??), you must create a state drawable as the background of your list items. In the "selected" state, set the drawable to be transparent (@null or #00000000). This way, when an item is selected, the selector will show.

Romain Guy
Can you please add more detail? Do I create the drawable in a file of the drawable folder? Is that drawable 100%, 50% transparent? How do I get the selected state? I am sure this is a useful answer (and that's why I bother) but it is still very vague for someone starting on this. Thanks!
Fabio Milheiro
A: 

Romainguy has the correct answer, but it was a bit unclear to me so I'll explain exactly what needs to be done. You have to create a different state drawable for each color you want to use. For example: list_selector_read.xml and list_selector_unread.xml. All you need to do is set everything to transparent except the android:state_window_focused="false" item. Then when you are drawing your list you call setBackgroundResource(R.drawable.list_selector_unread/read) for each row. You don't set a listSelector on the ListView at all. That will maintain the default selector for your particular flavor of Android.

Hawkee
A: 

Ok, I got it to work like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_pressed="false" android:drawable="@color/BackgroundColor" />
    <item android:drawable="@color/transparent" />
</selector>

YMMV!

Adam
A: 

mAgendaListView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { //view.setBackgroundColor(Color.RED);

         for(int i=0; i<parent.getChildCount(); i++)
         {
          if(i == position)
          {
           parent.getChildAt(parent.getChildCount() - i -1).setBackgroundColor(Color.BLUE);
          }
          else
          {
           parent.getChildAt(parent.getChildCount() - i - 1).setBackgroundColor(Color.BLACK);
          }

         }
Mbolland