以下代码很好地动画将新字符串添加到列表框的末尾

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。 有谁知道如何做到这一点?

有帮助吗?

解决方案

To work around the fact that ListBox1.InsertObject and ListBox1.Items.Insert don't work you can do the following

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;

but is a bit ridiculous. It (eventually) adds the string in position 0 if there is no item selected, otherwise adds it before the selected item. This solution reminds me too much of Bubble Sort. You will need to add the math unit to your uses clause for the max function to work.

This does indeed seem to be a bug in FireMonkey (check Quality Central #102122), However I suspect a future FireMonkey update will fix this. If anyone can see a better way of doing this....

I've also made a movie about this for those who are interested, which illustrates things more clearly.

其他提示

This should work, but it does nothing:

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

If I then call the following, I get an access violation:

ListBox1.Realign;

In fact, even this gives me an AV:

ListBox1.Items.Insert(0, 'hello');
ListBox1.Realign;

But this adds one, of course:

ListBox1.Items.Add('hello');

A bug perhaps?

Instead of

l.Parent := ListBox1;

use

ListBox1.InsertObject(Index, l);

where Index is the insertion position.

(Untested but from reading the sources it should work).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top