How to change background color of selected items in ListView?

I saw a lot of simillar questions, and each answer is very specific to the problem and does not have a direct answer, or I found tutorials that show how to create a checkbox marked on the selected items. And it's hard for me to figure out how to do this from these codes.

I follow the tutorial found here , and what a beat, how my code looks only different.

I want to have a multiple ListView when the item selected by the background color is resized to mark the items I selected.

Maybe I can do this with a custom selector? I realized that the general way is to keep the position of the selected one and do something in the getView function. I saw people creating ViewHolder, but I really did not understand what it should do with anything. Can someone please help me?

Thanks in advance Eric

+3
source share
3 answers

Well, I finally solved it, hope this helps someone:

What I did was created ArrayList<Integer>, which saves all the positions of the selected elements and switches the background colors to clicks.

In my adapter, I define:

public ArrayList<Integer> selectedIds = new ArrayList<Integer>();

Following method:

    public void toggleSelected(Integer position)
{
    if(selectedIds.contains(position))
    {
        selectedIds.remove(position);


    }
    else
    {
        selectedIds.add(position);
    }
}

which adds / removes elements from an ArrayList

In my getView method:

            if (selectedIds.contains(position)) {
            convertView.setSelected(true);
            convertView.setPressed(true);
            convertView.setBackgroundColor(Color.parseColor("#FF9912"));
        }
        else
        {
            convertView.setSelected(false);
            convertView.setPressed(false);
            convertView.setBackgroundColor(Color.parseColor("#000000"));
        }

This checks if the position is stored in the ArrayList. if so, color it as selected. if not, vice versa.

, OnItemClick, :

    ((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));

YourAdapter ListView

, , :)

+13

:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/android:transparent" />
    <item android:drawable="@drawable/listitem_normal" />
</selector>

: ListView

0

XML. WRT API 15. :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/item_selection">
    <ImageView />
    <.. />
</LinearLayout>

item_selection.xml res/drawable-hdpi ( Android Studio 0.8.14):

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

    <item android:drawable="@android:color/holo_blue_dark" android:state_selected="true" />
</selector>
0

All Articles