delphidelphi-10.3-riotlistview

How to get range of OnData indexes from Virtual Listview


I'm dealing with large text files (bigger than 100MB). I'm using a TListView (OwnerData=True). ListView's OnData event gives me required items one by one.

procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
  Item.Caption:=MyStringList.strings[Item.Index];
end;

But I need the full range of required items at once (after scrolling). I need this conceptual event:

procedure TForm1.ListView1DataRange(Sender: TObject; ItemIndexStarts: int64; ItemIndexEnds: int64);
begin
  LoadMyItemsByRange(ItemIndexStarts,ItemIndexEnds);
end;

LoadMyItemsByRange is easy to implement. How can I get ItemIndexStarts and ItemIndexEnds values? Any idea is appreciated.


Solution

  • The OnData event is for returning data for a specific item only, there is no range provided. You are correct to use the Item.Index property to know which item is being requested.

    If you want to pre-load your data ahead of time, you need to use the OnDataHint event, in addition to the OnData event. The OnDataHint event gives you a range of indexes that are the ListView's best guess to which item(s) are likely to be needed soon, due to scrolling, drawing, etc.

    However, that being said, there is no guarantee that the OnData event will ask only for items that were previously requested by the OnDataHint event. That is why it is called a hint. Your OnData handler needs to be prepared to load items that have not been loaded yet. And, for good measure, it is a good idea to have the first few items and the last few items always loaded at all times if possible, as the ListView refers to those items fairly often.