无限滚动 VirtualTreeView

Infinite scrolling VirtualTreeView

有没有办法使用virtualtreeview实现无限滚动?

我想一次加载一定数量的数据库记录,并在用户向下滚动时将它们添加到 virtualtreeview。但我不确定如何触发添加新行。

您可以这样处理OnScroll事件并检查滚动条是否到达终点:

type
  // this interposer class is used to publish the RangeY property
  TVirtualStringTree = class(VirtualTrees.TVirtualStringTree)
  public
    property RangeY;
  end;

procedure TForm1.VirtualStringTreeScroll(Sender: TBaseVirtualTree; DeltaX,
  DeltaY: Integer);
var
  Tree: TVirtualStringTree;
begin
  // if the vertical scroll occurred, then...
  if DeltaY <> 0 then
  begin
    // just a helper variable
    Tree := TVirtualStringTree(Sender);
    // if the client height without the top offset equals, or exceeds (actually, it should
    // never exceed; just for sure) the virtual tree height, then we reached the bottom of
    // the tree, so...
    if Tree.ClientHeight - Tree.OffsetY >= Integer(Tree.RangeY) then
    begin
      // the scrollbar reached the end of the tree; now fetch your data and add some nodes
      // (ideally as a thread task showing some fancy animation; the following is just for
      // example)
      ShowMessage('Fetch your data...');
      Tree.RootNodeCount := Tree.RootNodeCount + 50;
    end;
  end;
end;