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

问题描述 投票:1回答:1

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

delphi listbox firemonkey
1个回答
0
投票

好,让我们分解一下,首先我们去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设置到适当的位置来移动VScrollBar.Value

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

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

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

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函数并从中获得启发。上面的代码适用于添加到滚动框中的简单项目。如果您对边距或填充有任何问题,只需改进公式即可解决。

© www.soinside.com 2019 - 2024. All rights reserved.