检测何时 Delphi FMX ListBox 滚动到底部?

Detect when Delphi FMX ListBox is scrolled to the bottom?

我需要检测用户何时向下滚动到 ListBox 的底部,以便我可以获取接下来的 25 个项目以显示在 listBox 中,有什么提示吗?

好的,让我们分解一下,首先我们转到 FMX.ListBox 单元

中的 ScrollToItem
procedure TCustomListBox.ScrollToItem(const Item: TListBoxItem);
begin
  if (Item <> nil) and (Content <> nil) and (ContentLayout <> nil) then
  begin
    if VScrollBar <> nil then
    begin
      if Content.Position.Y + Item.Position.Y + Item.Margins.Top + Item.Margins.Bottom + Item.Height >
        ContentLayout.Position.Y + ContentLayout.Height then
        VScrollBar.Value := VScrollBar.Value + (Content.Position.Y + Item.Position.Y + Item.Margins.Top +
          Item.Margins.Bottom + Item.Height - ContentLayout.Position.Y - ContentLayout.Height);
      if Content.Position.Y + Item.Position.Y < ContentLayout.Position.Y then
        VScrollBar.Value := VScrollBar.Value + Content.Position.Y + Item.Position.Y - ContentLayout.Position.Y;
    end;
    if HScrollBar <> nil then
    begin
      if Content.Position.X + Item.Position.X + Item.Margins.Left + Item.Margins.Right + Item.Width >
        ContentLayout.Position.X + ContentLayout.Width then
        HScrollBar.Value := HScrollBar.Value + (Content.Position.X + Item.Position.X + Item.Margins.Left +
          Item.Margins.Right + Item.Width - ContentLayout.Position.X - ContentLayout.Width);
      if Content.Position.X + Item.Position.X < 0 then
        HScrollBar.Value := HScrollBar.Value + Content.Position.X + Item.Position.X - ContentLayout.Position.X;
    end;
  end;
end;

现在如您所见。该过程检查许多值(边距、填充、顶部……),然后通过将 VScrollBar.Value 设置到适当的位置来移动 VScrollBar

您想知道垂直滚动条何时到达底部。

所以我们使用与列表视图的其他答案相同的想法。

我们首先添加这个 hack 来暴露 TListBox 的私有和受保护部分 class

TListBox = class(FMX.ListBox.TListBox)
  end;

将其添加到列表框所在的表单,然后使用 VScrollChange(Sender: TObject); 事件并对 if 条件进行反向工程。

像这样的东西对你有用

procedure TForm1.ListBox1VScrollChange(Sender: TObject);
var
  S:single;
begin
  S:= ListBox1.ContentRect.Height;

  if ListBox1.VScrollBar.ValueRange.Max = S + ListBox1.VScrollBar.Value then
    Caption := 'hit'
  else
    Caption := 'no hit';
end;

在尝试解决这些类型的问题时,总是寻找 ScrollToControl 函数并从中获得灵感。上面的代码使用添加到滚动框的简单项目。如果您对边距或填充有任何问题,只需改进公式即可解决。