Android listview selected background not working in 2.3

I am trying to get my application to work fine in 2.3 (it works fine in 4.0+), and one problem that I encountered is that in my list I can not get the background for the selected item to change. I'm not sure what I need to change - does anyone know?

Here is the list:

<ListView
    android:id="@+id/score_list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/keyboard"
    android:layout_below="@+id/sort_header"
    android:choiceMode="singleChoice"
    android:divider="#CCCCCC"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/selector" />

Here's a selector that works in 4.0+ (there is no color change in 2.3):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/highlight"/>
    <item android:state_pressed="true" android:drawable="@color/highlight"/>
    <item android:state_activated="true" android:drawable="@color/highlight"/>
    <item android:state_selected="true" android:drawable="@color/highlight"/>
</selector>

I really don't need all 4 of them, but I wanted to try everything.

+5
source share
4 answers

I had exactly the same problem. I did not find a way to do this in XML, but I found a workaround in the code. The following code is tested in an application that supports API level 7 +

ListView:

public class ListViewAdapter extends BaseAdapter {
  private int selectedItemPosition = -1;

  // your code

  public void selectItem(int i) {
    selectedItemPosition = i;
  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {

    // your code

    if (i == selectedItemPosition) {
      // set the desired background color
      textView.setBackgroundColor(context.getResources().getColor(R.color.highlight));
    }
    else {
      // set the default (not selected) background color to a transparent color (or any other)
      textView.setBackgroundColor(Color.TRANSPARENT);
    }
    return view;
  }
}

, OnItemClickListener onItemClickMethod:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  // select the item
  ((ListViewAdapter) listview.getAdapter()).selectItem(position);
  // force redraw of the listview (invalidating just "view" is not enough)
  ((ListView) parent).invalidateViews();

  // your code
}

. , , , onItemClick(), .. selectItem(), invalidateViews(). invalidateViews() notifyDataSetChanged().

ListSelector , . API 7 8 .

+7

android:cacheColorHint="@null. .

+1

android:state_activated Presented in API Level 11.

See this link Resources for drawing

Update

I use this in my application (API level 8)

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item 
 android:state_selected="false"
    android:state_pressed="false" 
    android:state_focused="false"
    android:drawable="@color/normal"/> 

<item android:state_pressed="true"
        android:drawable="@color/highlight"/>

<item android:state_selected="true"
     android:state_pressed="false"
     android:drawable="@color/highlight"/>

<item android:state_focused="true"
     android:state_pressed="false"
     android:drawable="@color/highlight"/>
</selector>
0
source

As written in AwdKab's answer, it android:state_activatedis introduced at API level 11. The solution is to implement an interface Checkablefor your top Viewitem list layout, see my post here .

0
source

All Articles