以下代码很好地以动画方式将新字符串添加到列表框的末尾
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;
该项目展开并淡入。但是,我希望能够将字符串添加到 ListBox 中的任意位置 - 实际上是在当前的 ItemIndex 处。 有谁知道该怎么做吗?
最佳答案
要解决 ListBox1.InsertObject
和 ListBox1.Items.Insert
不起作用的问题,您可以执行以下操作
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;
但是有点可笑。如果没有选择任何项目,它(最终)会将字符串添加到位置 0,否则将其添加到所选项目之前。这个解决方案让我想起了太多Bubble Sort 。您需要将数学单位添加到您的 use 子句中才能使 max 函数发挥作用。
这确实似乎是 FireMonkey 中的一个错误(检查 Quality Central #102122 ),但是我怀疑 future 的 FireMonkey 更新将修复此问题。如果有人能找到更好的方法......
我也 made a movie对于那些感兴趣的人来说,这更清楚地说明了事情。
关于delphi - 在 FireMonkey 中动画添加字符串到列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9025465/