0

Now I am using:

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

where bg_list_item.xml:

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

<item android:state_selected="true">
    <shape android:shape="rectangle">
        <gradient android:startColor="#0000ff"/>
    </shape>
</item>

<item android:state_focused="false"
      android:state_pressed="false"
      android:state_selected="false">
    <shape android:shape="rectangle">
        <gradient android:startColor="#ff0000"/>
    </shape>
</item>

BUT first item is not working and selected item is not highlighting sometimes on scroll (when I am near top/bottom of my listview) and never on listView.setSelection(index).

Does I must to highlight current item programmaticaly?

Sviatoslav
  • 1,301
  • 2
  • 17
  • 42

4 Answers4

3

This is because there is not a selected item in touchmode. See here for an explanation.

You can do it with a selector, but it requires three things...

1) You must make the list row chooseable

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

2) Add to your selector file a condition for the activated state:

    <item android:state_activated="true" >
        <shape android:shape="rectangle"> 
        <gradient android:startColor="#0000ff"/>

3) You need create a custom adapter to track the state of each position in your list so you can maintain the proper state when scrolling.

EDIT

breceivemail's answer is more suited to your needs. Mine, while it would work, would work better for a multiple checked item type list (by changing it to CHOICE_MODE_MULTIPLE), and is more work for what you need than is necessary.

Barak
  • 16,318
  • 9
  • 52
  • 84
2

define a custom adapter and in its getView() method:

private int selectedItem;

// is used outside the adapter for example OnItemClickListener
public void setSelectedItem(int position) {
    selectedItem = position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   //...
   // set selected item
    LinearLayout activeItem = (LinearLayout) rowView;
    if (position == selectedItem)
    {
       activeItem.setBackgroundResource(R.drawable.background_dark_blue);
    }
    else
    {
       activeItem.setBackgroundResource(R.drawable.border02);
    }
   //...
}

background_dark_blue.xml

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

    <item>
<shape android:shape="rectangle" >

            <solid android:color="@color/lightBlue6" />
            <!-- border width and color -->

            <stroke
                android:width="2dp"
                android:color="#FF0404B4" />

            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp" />
        </shape></item>

</layer-list>

border02.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" android:layout_width="wrap_content">
            <stroke android:width="0.5dp" android:color="#FF000000" />
            <solid android:color="#FFFFFFFF" />
            <padding android:left="0dp" android:top="0dp" android:right="0dp"
                android:bottom="0dp" />
            <corners android:radius="0dp" />
        </shape>
    </item>


</layer-list>
Bob
  • 22,810
  • 38
  • 143
  • 225
1

"android:listSelector" property is used to specify item background when it is clicked. To highlight the first item or the one selected after click you will need to do it programmatically.

k_shil
  • 2,108
  • 1
  • 20
  • 25
0

ListViews by default don't have a chioceMode set (it's set to none), so the current selection is not indicated visually. Playing around with the choiceMode attribute will get you to the desired aspect.

You could take a look at this solution as well, maybe it will work for you too.

Community
  • 1
  • 1
rekaszeru
  • 19,130
  • 7
  • 59
  • 73