在FireMonkey中实现向ListBox添加字符串的动画效果

8
以下代码可以很好地为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处。有人知道如何做吗?

3个回答

5
为了解决 ListBox1.InsertObjectListBox1.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添加字符串,否则在所选项目之前添加字符串。这个解决方案让我太想起了冒泡排序。您需要将数学单元添加到使用子句中才能使max函数正常工作。

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

我还制作了一部电影,介绍得更清楚。


它运行良好。除非您有一个非常大的列表,否则打乱列表相当快。 - LU RD

2

这应该可以工作,但实际上什么也没有发生:

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

如果我调用以下内容,则会出现访问冲突:
ListBox1.Realign;

今日免费次数已满, 请开通会员/明日再来
ListBox1.Items.Insert(0, 'hello');
ListBox1.Realign;

当然,这会增加一个:
ListBox1.Items.Add('hello');

可能是一个bug?

是的,我得到了相同的结果 - 我同意 - 可能是一个错误。 - Alister
如上所述,Listbox.items.Insert 目前存在问题。或许在更新4中修复? - Warren P

0

不要使用

l.Parent := ListBox1;

使用

ListBox1.InsertObject(Index, l);

其中Index是插入位置。

(未经测试,但从阅读源代码来看应该可以工作)。


1
ListBox1.InsertObject(0, l)不起作用并且会导致访问冲突,尽管如果我删除与动画相关的代码,listbox1.count会增加但是没有任何可见变化。然而,ListBox1.AddObject(l)可以正常工作(虽然结果与我的原始代码相同)。 - Alister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接