0
votes

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.

1
@KenWhite, please uncheck the question as duplicate, as they are different.wBB
Thanks @TomBrunberg . I don't know if I'm happy or even more upset with your information, after all I have to let it go that configuration... :) Anyway, you help me a lot not to waste any more time on it.wBB
I've reopened the question and cleaned up the comment clutter.Ken White
@wBB Maybe this question might help? I don't know Delphi, but this answer to that question looks appropriate. This looks like a possible real duplicate question (with no answer). And just for laughs this is an offsite question with the same wrong answer as you got here (previously).robinCTS
Thanks for your attention @robinCTS . In my example I have to use a multiplatform Delphi framework, called Firemonkey. In your examples are used VCL controls or a custom ListView (which seems to be pretty cool!), but I can't use any controls that aren't native from Delphi. Thank you again.wBB

1 Answers

0
votes

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;