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

Asked

Viewed 1,398 times

1

In Delphi 10 with Firemonkey, at runtime I need that when clicking on a list item of a Tlistview, the background color of this item be changed and remain so even if I click on another item. Then, even if you click on another item, its background color will also be changed and will remain so.

I’ve seen them say to use Tstylebook and change the style of Listview items, but I couldn’t assemble the code to run in Runtime.

I imagined something like this, but it didn’t 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 does not give error, but does not work. Also I need to execute the color change only in the clicked item.

2 answers

1

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

this is the example code

form show event

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;

inserir a descrição da imagem aqui

0

Look this is not possible to be done in Runtime because Listview itself in Firemonkey does not allow it to be done in Runtime only in design time you can change the backgroundCollor of listview via Stylebook, but not in the way you want, It could only be done if Listview was VCL but FMX was not, at least to my knowledge it is so unfortunately. You can find more details here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.