Reputation: 2136
What I have: A custom listview with a custom adapter, with a custom ListViewItem (which extends LinearLayout)
What i want to do: When the listbox item is selected, I want to change the background drawable of the selected view.
What Happens: Currently nothing. The Toast does appear as it should, but the Background drawable does not change. I tried calling View.Invalidate() but that doesn't seem to do anything either.
Here is the snippet of code that is supposed to change the drawable:
public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3)
{
Card card = CardVM.GetCurrentCards().get(index);
card.setBlocked(!card.isBlocked());
if (card.isBlocked())
{
view.setBackgroundResource(R.drawable.bluegradient);
Toast.makeText(this, "Card is now Blocked", 1).show();
}
else
{
view.setBackgroundResource(R.drawable.greengradient);
Toast.makeText(this, "Card is now Available", 1).show();
}
}
The toggle functionality does work, if you click on it the first time, it will say "card is now blocked". The second time shows "Card is now available"... and so on. The setBackgroundResource simply does not work.
Also, when the list view is populated, the background colors are set correctly (using the same resources and the same function).
Thanks in advance.
UPDATE
It appears the background DOES change, but ONLY if the screen is forced to re-draw it. For example, when you click on the first item in the list to turn it blue, you need to scroll down (to get it off the screen) then scroll back up. THEN it turns blue. Can we avoid this somehow?
Upvotes: 2
Views: 4357
Reputation: 8533
use AdapterView.OnItemSelectedListener()
and maybe something like view.setBackgroundDrawable(getResources().getDrawable(R.drawable.listview_item_selected));
Edit:
invalidate the layout not the individual view.
Upvotes: 1