问题
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 circle buttons. I have tried setting the the list item's selected/pressed state true, or setting its background color on onListItemClick
but when I do this, my list items stay clicked even after clicking other items. How can I unset the background color of the previously clicked items in the list? Or there is a different way to do this?
回答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));
...
}
来源:https://stackoverflow.com/questions/11600996/android-how-to-retain-clicked-list-items-pressed-selected-state-until-pressing