以下代码很好地动画了在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;
项目扩展并淡入。但是我希望能够将字符串添加到ListBox中的任意位置 - 实际上是在当前的ItemIndex中。 有谁知道怎么做?
答案 0 :(得分:4)
要解决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。您需要将数学单元添加到uses子句中才能使max函数起作用。
这确实是FireMonkey中的一个错误(检查Quality Central #102122),但我怀疑未来的FireMonkey更新会解决这个问题。如果有人能够看到更好的方法......
对于那些感兴趣的人,我也made a movie对此有所了解,这更清楚地说明了事情。
答案 1 :(得分:2)
这应该可行,但它什么都不做:
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');
也许是一个错误?
答案 2 :(得分:0)
而不是
l.Parent := ListBox1;
使用
ListBox1.InsertObject(Index, l);
其中Index是插入位置。
(未经测试但是从阅读它应该起作用的来源)。