0

I have a ListView and I wanted to change the default blue highlight when it is set to listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); to a custom image not only when the item is selected/pressed but also stays selected/pressed by this code:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    v.setBackgroundResource(0);
    view.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.list_item_highlight_short));
    v = view;
}

The problem is that if I have 20 item in my ListView, some items that are not within the view are still highlighted and not included in v.setBackgroundResource(0);

How could I remove the BackgroundResource of all items in the ListView so that I could set the clicked item a custom image background to indicate "selected" state?

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

3 Answers3

2

Try to override getView method of your adapter. And do this work in that method.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.Your_listview_layout, null);
    }

    if (listview.isItemChecked(position)) {                          
        v.setBackgroundResource(R.drawable.list_item_highlight_short);                                      
    } else {
        v.setBackgroundResource(0);                     
    }
    return super.getView(position, v, parent);
}
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • How can i get hold of `listview` reference in the line `if (listview.isItemChecked(position)) {` where my `getView` is inside my custom ArrayAdapter class and my ListView is at my calling Activity. And also there's an error `Arrayadapter requires the resource ID to be a textview` in the line `return super.getView(position, v, parent);` since my item_view has not only TextView but other elements as well. – Compaq LE2202x Jul 25 '13 at 06:23
  • Modify your `ArrayAdapter` like [this](http://stackoverflow.com/questions/9280965/arrayadapter-requires-the-resource-id-to-be-a-textview-xml-problems/9282069#9282069) answer. And try again. – Gunaseelan Jul 25 '13 at 06:56
1

Please try to use the ListSelecter Property of your listview

for example:

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="#0000ff" >

    </ListView>
dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • Instead of color code you can use drawable item as android:listSelector="R.drawable.list_item_highlight_short" – dinesh sharma Jul 25 '13 at 04:56
  • This is partly working but when I scroll the ListView rapidly with about 20 items, the image background I set is somehow dettached on the selected item itself and remains at the top side of the ListView when I scroll up or bottom when I scroll down. But if the selected item is within the view of the ListView, the background image will snap back to the selected item position. – Compaq LE2202x Jul 25 '13 at 06:35
0

Store the clicked item position in your adapter. Like in above function adapter.setPosition(position). Then in getView function of adapter, check if the position is selected one and then change backgroung accordingly

Pulkit Sethi
  • 1,325
  • 1
  • 14
  • 22
  • What is `adapter` in `adapter.setPosition(position)`? My adapter class extends ArrayAdapter but does not have `setPosition` method. Can you show me code? – Compaq LE2202x Jul 25 '13 at 06:24
  • I use base adapter and add my own methods in it as array adapter is for kids. You can easily find examples for it.technique us to save the selected item in adapter and in getview change back ground as needed – Pulkit Sethi Jul 25 '13 at 08:28