0

I am trying to use a selector to highlight a selected row in my ListView. I need it to remain highlighted after i have released the press. Symbolizing the highlighted row is the row selected.

Currently the text changes color by default. I need to change the background color.

I have the ListView in a fragment that is inside of a DrawerLayout. When i select a device, the DrawerLayout closes. When i open the DrawerLayout again, the background of the device i selected is not changed. The text of the device selected is a different color, but not the background.

I have tried numerous suggestions here on SO and have found nothing that works. I am targeting API-17 and minsdk is API-17.

Here are some posts i have tried with no success.

Let me know if you need any further details, thanks.

Here is my code(with the last thing i tried):

device_list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/device_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:background="@drawable/device_selector" >
</ListView>

device_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_activated="true"
        android:drawable="@drawable/device_selected"/>
    <item
        android:drawable="@drawable/device_not_selected"/>
</selector>

strings.xml
<drawable name="device_selected">#777777</drawable>
<drawable name="device_not_selected">#555555</drawable>

DeviceListFragment
public class DeviceListFragment extends Fragment implements OnItemClickListener {
    public View onCreateView(...) {
        View view = inflater.inflate(R.layout.device_list_fragment, container, false);
                this.devicesArrayAdapter = new DevicesArrayAdapter(this.getActivity());

        this.deviceListView = (ListView) view.findViewById(R.id.device_list_view);
        this.deviceListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        this.deviceListView.setAdapter(this.devicesArrayAdapter);
        this.deviceListView.setOnItemClickListener(this);

        return view;
    }

    public void onItemClick(...) {
        this.deviceListView.setItemChecked(position, true);
    }
}

DevicesArrayAdapter
public class DevicesArrayAdapter extends ArrayAdapter<Device> {
    public View getView(...) {
        View rowView = layoutInflater.inflate(R.layout.device_element, parent, false);
        return rowView;
    }
}
Community
  • 1
  • 1
prolink007
  • 33,872
  • 24
  • 117
  • 185

2 Answers2

1

Your adapter is the problem - You only inflate the view and return it. If you want the background of your view to remain a different color, you should set a flag that indicates whether the rowView should be changed or not. A sparse array is a nice way to do this. Otherwise, every time you show the DevicesArrayAdapter, it gets the default view, no background change.

Something like this:

getView(...) {
    View rowView = layoutInflater.inflate(R.layout.device_element, parent, false);

    If (arrayViews[position] == CHANGE_BACKGROUND){

         rowView.setBackground = NEW_COLOR;
    }

    return rowView;
}
Martin
  • 4,711
  • 4
  • 29
  • 37
  • I thought the purpose of the selector was to assist in changing parts of the view based on what type of selection was made? Just using the selector will not allow the change to persist? – prolink007 Jan 26 '15 at 19:28
  • Ok. I have done this. The only way this is working for me, is to make a call to `myArrayAdapter.notifyDataSetChanged();` in the `onItemClick()` method. Is this correct? – prolink007 Jan 26 '15 at 19:36
  • I decided to go with a solution i already had that was similar to this solution. It is not exactly what i was expecting, but it will work for now. Thanks – prolink007 Jan 26 '15 at 20:18
1

In your DeviceFragment change

public void onItemClick(...) {
    this.deviceListView.setItemChecked(position, true);
}

To

public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
    view.setSelected(true);
}
ayz4sci
  • 2,220
  • 1
  • 16
  • 17
  • I tried this already, it did not work. Here is the post that i tried that was related to this answer http://stackoverflow.com/a/16190228/427763 – prolink007 Jan 26 '15 at 19:44
  • Try this too In your device_selector.xml change android:state_activated="true" To android:state_selected="true" – ayz4sci Jan 26 '15 at 19:54
  • I tried that already, it did not work. If you take a look at the post i linked in my previous comment, you will see the exact changes i made to try and get it to work. It still failed to persist the highlight. – prolink007 Jan 26 '15 at 19:55