Android Listview - item selection/unselection

Asked

Viewed 39 times

0

I have an application that has a custom Listview. When you click on an item in this Listview, the background color changes to indicate that it is selected.

In the same Activity I also have an Edittext. The problem is that when I "click" on Edittext the selection of the item in Listview is undone, that is, the item returns to its original background color. I don’t want that to happen.

This is the selector code I use to change the background color of the item (list_item_selector.xml):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/item_list_selected"/>
    <item android:state_pressed="true" android:drawable="@drawable/item_list_pressed"/>
    <item android:state_focused="true" android:drawable="@drawable/item_list_focused"/>
    <item android:drawable="@drawable/item_list_normal"/>
</selector>

In the item layout I define the item background as the list_item_selector.xml file (item_request_list.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_all_magim"
    android:orientation="horizontal"
    android:weightSum="5"
    android:baselineAligned="false"
    android:background="@drawable/list_item_selector">

...... o resto do código

</LinearLayout>

Finally, this is the implementation of onItemClick():

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // salva a posicao do item selecionado
    lastPositionSelected = position;
    // salva o objeto relativo ao item selecionado
    servicoSelected = servicos.get(position);
    view.setSelected(true);
}

Does anyone have any idea how to prevent item selection from being undone (background color back to original color) when Edittext is triggered?

  • tries calling Adapter.notifyDataChanged();

  • ramaral No, it is not a duplicate. They are different problems. Read again, my problem is that when I call Edittext the item selection in Listview is undone. How to prevent this from happening?

  • Carlos Bridi, I don’t understand your suggestion.

  • Eliminate the line view.setSelected(true); and do what is said in reply that I mentioned.

  • I was using the "state_focused" attribute instead of the "state_activated" attribute. Now it’s working, thanks!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.