Skip to content Skip to sidebar Skip to footer

Android: How To Retain Clicked List Item's Pressed/selected State Until Pressing Other Items On List

I want my list items to retain its pressed/selected state until I press other items on list. While typing this, I thought that I want it to work like RadioButtons without the circl

Solution 1:

Try this

Step 1: Try to reset background color(i.e your original background color) to all list items using for loop in onItemClickListener().

Step 2 :Set Different background color for selected one.

If you using Customized list View then it will be really easy.

EDIT Customized List View Tutorial

Custom ListView and this

Video - youtubeVideo

Hope it helps

EDIT

Here's what I did: I iterated through the visible list to set the each item's background to transparent the setting the selected item's background a different color. This way, the color sticks until I press another item.

protected void onListItemClick(ListView l, View v, int position, long id) { int childCount = l.getChildCount();

for (int i = 0; i < childCount; i++)
{
    View listItem = l.getChildAt(i);
    if(listItem != null )   listItem.setBackgroundColor(0x00000000);
}
v.setBackgroundColor(getResources().getColor(R.color.pressed));
...

}


Post a Comment for "Android: How To Retain Clicked List Item's Pressed/selected State Until Pressing Other Items On List"