【问题标题】:How can I speed up filling this TListBox?我怎样才能加快填充这个 TListBox?
【发布时间】:2016-04-12 14:22:28
【问题描述】:

问题

我有 2 个不同的 json arrays,看起来像这样:

1 - 事件列表

{"0000:第一个事件名称","0001:第二个事件名称","0002:第三个事件名称"}

2 - 可用事件列表

{"0001","0002"}

然后我需要使用复选框生成一个包含可用事件的 ListBox:


  • [ ] 第一个事件名称
  • [x] 第二个事件名称
  • [x] 第三个事件名称

给定解决方案

procedure TFormHome.GetEvents(Sender: TObject);
var

    K: Integer;
    Z: Integer;
    ListCount_Events : Integer;
    AvailableList_Count : Integer;

    lb_item: TListBoxItem;

    event_code  : string;
    event_code_1: string;
    event_name  : string;

begin

    // Check if the JSON responses are not nil
    if ((json_response_events <> nil) and (json_response_available_events <> nil)) then
    begin

        ListCount_Events := json_response_events.Count;

        // Get Available List Count
        AvailableList_Count := json_response_available_events.Count;

        try
            // Run a for loop to create the events based on ListCount_Events
            for K := 0 to (ListCount_Events - 1) do
            begin

                // Get complete Event Code
                event_code_1 := StringReplace(json_response_events.Items[K].ToString.Split([':'])[0], '"', '', [rfReplaceAll]);

                // Get complete Event Name
                event_name := StringReplace(json_response_events.Items[K].ToString.Split([':'])[1], '"', '', [rfReplaceAll]);

                // Create the ListBoxItem
                lb_item := TListBoxItem.Create(self);

                // Assign it to the the ListBox component
                lb_item.Parent := lb_notifications;

                // ListBoxItem get the event name
                lb_item.Text := event_name;

                // Remove StyledSettings (Other)
                lb_item.StyledSettings := lb_item.StyledSettings - [TStyledSetting.Other];

                // Remove StyledSettings (FontColor)
                lb_item.StyledSettings := lb_item.StyledSettings - [TStyledSetting.FontColor];

                // Change TextSettings FontColor to default
                lb_item.TextSettings.FontColor := $FF626262;

                // Set selectable to false in order to not permit the user
                // to select multiple items on the List
                lb_item.Selectable := false;

                // Set the appropriated style
                lb_item.StyleLookup := 'listboxitemleftdetail';

                // Run a for loop to check the available events
                for Z := 0 to (AvailableList_Count) do
                begin

                        event_code := StringReplace(json_response_available_events.Items[Z].ToString, '"', '', [rfReplaceAll]);

                        if event_code_1.Contains(event_code) then
                        begin

                            if K < ListCount_Events then
                            begin

                                // Remove StyledSettings (FontColor)
                                lb_item.StyledSettings := lb_item.StyledSettings - [TStyledSetting.FontColor];

                                // Change TextSettings FontColor to available
                                lb_item.TextSettings.FontColor := $FF179ADF;

                                // Set the List CheckBox to checked
                                lb_item.IsChecked := true;
                            end;

                        end;
                end;

            end;

        finally
            begin
                // Call to List start at position 0
                lb_notifications.ItemIndex       := 0;
            end;
        end;

    end;
end;

我认为我的代码有异味,我想知道原因并学习做得更好。

欢迎提出合理的建议!

  • AndroidiOS 上加载列表需要太长

  • 当可滚动时,Android 上的速度非常慢(但iOS 上却没有)。

如何加快填写此列表的速度?

【问题讨论】:

  • 添加许多项目时,在列表框上使用 beginupdate / endupdate。否则,它将对每个新项目进行(缓慢)重绘。
  • BeginUpdate/EndUpdate 是一个很好的加载速度,但在 Android 上滚动仍然很慢。
  • FireMonkey TListBox 的滚动速度很慢,因为它的设计是每个 TListBoxItem 都是控件的容器。如果您希望在移动设备上滚动时获得更快的性能,请查看 TListView 或第 3 方 TListBox。

标签: android loops delphi listbox firemonkey


【解决方案1】:

按照@birger 的建议,用两行简单的代码解决了这个问题:

BeginUpdate;
//
GetEvents;
//
EndUpdate;

如果您使用Delphi 组件,如ListBoxMemo、ListView...,并且您添加或修改了很多项(行、节点...),组件的性能变得非常慢的。这是因为每次更改后都会在屏幕上重新绘制。

BeginUpdateEndUpdate 在添加、删除或插入项目时防止过度重绘并加快处理时间。

参考:http://www.festra.com/eng/tip-beginupdate.htm

【讨论】:

  • 事实上,通过插入项目(在我的情况下为 8000)beginupdate..endupdate 没有帮助。唯一的变化是;表单/组件显示得非常快,有一些插入的项目,但插入仍在继续,您可以看到列表正在增长。与您使用“application.processmessages”的效果大致相同,但在这种情况下,您可以关闭表单/应用程序而无需等待插入完成(视觉上)。
猜你喜欢
  • 2021-11-21
  • 2021-08-01
  • 1970-01-01
  • 2011-09-27
  • 2021-07-31
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多