I know this has been discussed many times in questions like this question and this one. But I have to say it doesn't work for me.
What I wanted to achieve is to mark a ListView item through my CustomAdapter in a special situation. So, not through the user and click listeners. I have a working solution by setting a background drawable to the convertView.
Now I want to change this and let do a selector the job, which is the cleaner solution. But I can't get it to work. There is no problem to set the behavior for pressing an item and set a different color. But I can't mark it as selected after I pressed the item.
I tried different combinations with my listview_item_selector and listview_selector. And I think, that I have missed something very general. That's why I made a setup just to figure out how to mark an item after it was pressed.
Here's my list_item_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@android:color/transparent"/>
<item android:state_selected="true" android:drawable="@android:color/transparent"/>
<item android:drawable="@drawable/state_normal"/>
</selector>
And my listview_selector setted to the ListView as selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/state_pressed"></item>
<item android:state_selected="true" android:drawable="@drawable/selected_item"/>
</selector>
As I said state_pressed
is working. But state_selected
drives me nuts.
At least my layout for the custom items:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/listview_item_selector">
<TextView
android:id="@+id/list_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFEE"/>
<TextView
android:id="@+id/list_tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFEEEE"/>
</LinearLayout>
I would appreciate any suggestions.