How to change the background color of a list item in a ListView in Delphi 10

0

In Delphi 10 with Firemonkey, at runtime I need that when I click on a list item in a TListView, the background color of this item is changed and it stays anyway so I click on another item. Then, even if you click on another item, the background color of this will also change and will remain that way.

I've already been told to use TStyleBook and change the style of ListView items, but I could not mount the code to run in runtime.

I imagined something like this, but it did not work:

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;

This code gives no error, but does not work. Also I need to perform the color change only on the clicked item.

    
asked by anonymous 16.03.2018 / 17:30

2 answers

0

Look, this can not be done at runtime because the ListView itself in FireMonkey does not allow it to be runtime only at design time you can change the listview backgroundCollor via StyleBook, but not in the way that you want it, it could only be done if the Listview was VCL but FMX not, at least I know it is so unfortunately. You can find more details here .

    
12.04.2018 / 21:21
0

An option, if you are using the listview with the Appearence = DynamicAppearance property you can add a TImageObjectAppearance by filling the entire row of the listview and applying an image of ( 1px x 1px ) to the color you wishes

this is the example code

event show form

var
  nitem: TListViewItem;
  i: Integer;
begin
    for i := 0 to 50 do
    begin
      nitem:= ListView1.Items.Add;

      nitem.Data['image']:= ImageList1.Bitmap(TsizeF.Create(1,1), Random(2));
      nitem.Data['text']:=  'registro'+inttostr(i)
    end;
end;

    
20.07.2018 / 13:57