问题
I need to detect when the user has scrolled down to the bottom in a ListBox , so that I can fetch the next 25 items to show in the listBox, Any Tip?
回答1:
Okay so let us break this down, first we go to ScrollToItem in FMX.ListBox unit
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;
Now as you can see. the procedure checks for many values (margins, paddings, top, ....) and then moves VScrollBar
by setting VScrollBar.Value
to the appropriate position.
You want to know when the Vertical scroll bar has reached the bottom.
so we use the same idea as my other answer for the list view.
We first add this hack to expose the private and protected parts of TListBox class
TListBox = class(FMX.ListBox.TListBox)
end;
add that to the form where the listbox is and then use the VScrollChange(Sender: TObject);
event and reverse engineer the if conditions.
Something like this will work for you
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;
when trying to solve these types of problems always look for a ScrollToControl function and get the inspiration from there. The code above is working with simple items added to the scroll box. if you have any problems with margins or paddings just evolve the formula to cope with that.
来源:https://stackoverflow.com/questions/62123973/detect-when-delphi-fmx-listbox-is-scrolled-to-the-bottom