【问题标题】:Animating the addition of a string to a ListBox in FireMonkey动画将字符串添加到 FireMonkey 中的 ListBox
【发布时间】:2012-02-19 23:31:19
【问题描述】:

以下代码很好地动画化了将新字符串添加到 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。 有人知道怎么做吗?

【问题讨论】:

    标签: delphi animation listbox firemonkey tlistbox


    【解决方案1】:

    代替

    l.Parent := ListBox1;
    

    使用

    ListBox1.InsertObject(Index, l);
    

    其中 Index 是插入位置。

    (未经测试,但通过阅读来源应该可以工作)。

    【讨论】:

    • ListBox1.InsertObject(0, l) 不起作用并给出访问冲突,尽管如果我删除与动画相关的代码,listbox1.count 会增加,但没有明显变化。但是 ListBox1.AddObject(l) 可以正常工作(尽管给出的结果与我的原始代码相同)。
    【解决方案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');
    

    也许是一个错误?

    【讨论】:

    • 是的,我得到了相同的结果 - 我同意 - 可能是一个错误
    • 如上所述,Listbox.items.Insert 当前已损坏。也许在更新 4 中修复?
    【解决方案3】:

    要解决 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,否则将其添加到所选项目之前。这个解决方案让我想起了太多Bubble Sort。您需要将数学单元添加到您的 uses 子句中才能使 max 函数起作用。

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

    我也给有兴趣的人made a movie这个,这更清楚地说明了事情。

    【讨论】:

    • 它工作正常。除非您有一个非常大的列表,否则改组列表非常快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2012-03-19
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多