检测德尔福FMX列表框何时滚动到底部?



我需要检测用户何时在列表框中向下滚动到底部,以便我可以获取接下来的 25 个项目以显示在列表框中,任何提示?

好的,让我们分解一下,首先我们转到FMX中的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

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

因此,我们使用与列表视图的另一个答案相同的想法。

我们首先添加这个技巧来暴露 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功能并从中获得灵感。上面的代码正在使用添加到滚动框中的简单项目。如果您对边距或填充有任何问题,只需改进公式即可应对。

最新更新