动画将字符串添加到FireMonkey中的ListBox

Ali*_*ter 8 delphi animation listbox firemonkey tlistbox

以下代码很好地动画将新字符串添加到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;
Run Code Online (Sandbox Code Playgroud)

该项扩展并淡入.但是我希望能够将字符串添加到ListBox中的任意位置 - 实际上是在当前的ItemIndex中.有谁知道如何做到这一点?

Ali*_*ter 5

要解决这个问题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;
Run Code Online (Sandbox Code Playgroud)

但有点可笑。如果没有选择任何项目,它(最终)会将字符串添加到位置 0,否则将其添加到所选项目之前。这个解决方案让我想起了冒泡排序。您需要将数学单位添加到您的 use 子句中才能使 max 函数发挥作用。

这确实似乎是 FireMonkey 中的一个错误(检查 Quality Central #102122),但是我怀疑未来的 FireMonkey 更新将修复此问题。如果有人能看到更好的方法来做到这一点......

我还为那些感兴趣的人制作了一部关于此的电影,它更清楚地说明了事情。