【问题标题】:Detect when Delphi FMX ListBox is scrolled to the bottom?检测Delphi FMX ListBox何时滚动到底部?
【发布时间】:2020-09-24 18:20:23
【问题描述】:

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

【问题讨论】:

  • stackoverflow.com/a/46306950/7579632 你看到这个了吗?我认为这是同一个想法。
  • ListBox 和 ListView 有不同的属性、选项,ListBox 没有那个选项...
  • 我还没有检查列表框,但如果它有 scrolltoControl() 那么你需要的就在那里。
  • scrolltoControl()?,嗯,你的想法是什么?

标签: delphi listbox firemonkey


【解决方案1】:

好的,让我们分解一下,首先我们转到 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 类的私有和受保护部分

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

【讨论】:

  • 太棒了,但我注意到一些事情,当你用鼠标滚动时它不起作用......
  • 我刚试过,你是对的。显然VScrollChange 在使用鼠标滚轮时不会被调用。但是您可以使用相同的代码使用MouseWheel 事件,它会起作用。
猜你喜欢
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多