Delphi 在FireMonkey中将字符串添加到列表框的动画
下面的代码很好地为向列表框末尾添加新字符串设置了动画Delphi 在FireMonkey中将字符串添加到列表框的动画,delphi,animation,listbox,firemonkey,tlistbox,Delphi,Animation,Listbox,Firemonkey,Tlistbox,下面的代码很好地为向列表框末尾添加新字符串设置了动画 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;
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;
该项在中展开并淡入。但是,我希望能够将字符串添加到列表框中的任意位置—实际上是当前的ItemIndex。
有人知道怎么做吗?而不是
l.Parent := ListBox1;
使用
其中,Index是插入位置
(未经测试,但通过阅读源代码,它应该可以工作)。这应该可以工作,但它不起任何作用:
l := TListBoxItem.Create(ListBox1);
ListBox1.InsertObject(Max(ListBox1.ItemIndex, 0), l);
如果我随后调用以下命令,则会出现访问冲突:
ListBox1.Realign;
事实上,即使这样也给了我一个AV:
ListBox1.Items.Insert(0, 'hello');
ListBox1.Realign;
当然,这又增加了一点:
ListBox1.Items.Add('hello');
可能是一个bug?要解决
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,否则会将其添加到所选项目之前。这个解决方案让我想起了太多。需要将数学单位添加到uses子句中,max函数才能工作
这似乎确实是FireMonkey(检查质量中心)中的一个bug,但是我怀疑FireMonkey的未来更新会解决这个问题。如果有人能找到更好的方法
我还为感兴趣的人介绍了这一点,这更清楚地说明了这一点。ListBox1.InsertObject(0,l)不起作用,并给出了访问冲突,尽管如果删除与动画相关的代码,ListBox1.count会增加,但没有明显变化。然而,ListBox1.AddObject(l)工作正常(尽管给出的结果与我的原始代码相同)。是的,我得到了相同的结果-我同意-可能是上面提到的错误,ListBox1.items.Insert当前已损坏。可能在更新4中修复?它工作正常。除非你有一个非常大的列表,否则洗牌列表是相当快的。
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;