Reputation: 851
Using Firemonkey, I created a list with TListView
. I want to change the background color of the item when I click on it, AND the color has to be retained even if I click on another item in the list.
Example: If the user clicks Item AAA, its color is changed to green. After that, if the user clicks on another item in the list, Item BBB for example, this second item's color will also turn green, with the first item retaining it's green color.
I've tried this code, which gives no error, but also does not change the color.
procedure TForm1.lvListasItemClickEx(const Sender: TObject; ItemIndex: Integer;
const LocalClickPos: TPointF; const ItemObject: TListItemDrawable);
var Obj: TFmxObject;
begin
Obj := ListView1.FindStyleResource('itembackground');
if Obj <> nil then
begin
TColorObject(Obj).Color := TAlphaColorRec.Blue;
end;
end;
How do I change the background color of selected items?
EDIT: Change Alternating Colors in Firemonkey TListView is not a duplicate of this question, because it is about changing the default alternating color whilst here I want to change an individual item's color independently of other items.
Upvotes: 0
Views: 2048
Reputation: 812
My suggestion is to add TListItemImage to listview, use it as a marker or set its bounds to cover whole item and use it as a backgraund. Then when you click on item, change the image assigned to the listitemimage object of the selected item.
for example:
procedure TForm1.lvListasItemClickEx(const Sender: TObject; ItemIndex: Integer;
const LocalClickPos: TPointF; const ItemObject: TListItemDrawable);
begin
TListItemImage(TListViewItem(lvListasItem.Selected).Objects.FindDrawable('Image1')).Bitmap := Image1.MultiResBitmap.Items[1].Bitmap;
end;
Upvotes: 0