Animation of adding a row to a ListBox in FireMonkey

The following code nicely animates adding a new line to the end of a ListBox

procedure TForm6.AddItem(s: string); var l : TListBoxItem; OldHeight : Single; begin l := TListBoxItem.Create(Self); l.Text := s; OldHeight := l.Height; l.Height := 0; l.Parent := ListBox1; l.Opacity := 0; l.AnimateFloat('height', OldHeight, 0.5); l.AnimateFloat('Opacity', 1, 0.5); end; 

The element expands and disappears. However, I want to be able to add a string to an arbitrary place in the ListBox - actually in the current ItemIndex. Does anyone know how to do this?

+8
animation delphi firemonkey listbox tlistbox
source share
3 answers

To get around the fact that ListBox1.InsertObject and ListBox1.Items.Insert not working, you can do the following

 procedure TForm1.AddItem(s: string); var l : TListBoxItem; OldHeight : Single; I: Integer; index : integer; begin l := TListBoxItem.Create(nil); l.Text := s; OldHeight := l.Height; l.Height := 0; l.Opacity := 0; l.Index := 0; l.Parent := ListBox1; Index := Max(0, ListBox1.ItemIndex); for I := ListBox1.Count - 1 downto Index + 1 do begin ListBox1.Exchange(ListBox1.ItemByIndex(i), ListBox1.ItemByIndex(i-1)); end; ListBox1.ItemIndex := Index; l.AnimateFloat('height', OldHeight, 0.5); l.AnimateFloat('Opacity', 1, 0.5); end; 

but a little funny. It (in the end) adds the line to position 0 if the item is not selected, otherwise it is added before the selected item. This solution reminds me too much of Bubble Sort . You will need to add a math block to your sentence for the max function to work.

It really looks like a bug in FireMonkey (check Quality Central # 102122 ). However, I suspect that a future FireMonkey update will fix this, if someone can see a better way to do this ....

I also made a film about it for those interested, which illustrates things more clearly.

+4
source share

This should work, but does nothing:

 l := TListBoxItem.Create(ListBox1); ListBox1.InsertObject(Max(ListBox1.ItemIndex, 0), l); 

If I then call the following, I get an access violation:

 ListBox1.Realign; 

In fact, even this gives me AV:

 ListBox1.Items.Insert(0, 'hello'); ListBox1.Realign; 

But this, of course, adds:

 ListBox1.Items.Add('hello'); 

Perhaps a mistake?

+2
source share

Instead

 l.Parent := ListBox1; 

using

 ListBox1.InsertObject(Index, l); 

where Index is the insertion position.

(Unverified, but from reading sources it should work).

0
source share

All Articles