对 TListView 对象内的项目进行分组?
-
02-01-2020 - |
题
我试图对 TListView 对象内的项目进行分组,但我找不到负责对对象进行分组的类,我也无法在文档中找到此类。
- 负责对 TListView 对象内的项目进行分组的类是什么,以及如何正确使用它?
平台为 Firemonkey ( Android/iOS) / Delphi XE6
解决方案
我相信你指的是 TListGroups
, ,一个包含 TListGroup
项目。有一个 演示 Delphi 文档中提供。
不幸的是,它仅在 VCL 中可用,而在 FMX 中不可用,因为底层功能是 Windows ListView 控件的一部分, TListView
包裹。
在 FMX 中您可以得到的最接近的是使用 TListBox
和一个 TListBoxGroupHeader
, ,多设备教程中对此进行了介绍 使用 ListBox 组件显示表格视图(iOS 和 Android) 在里面 文档维基:
procedure TForm1.FormCreate(Sender: TObject);
var
c: Char;
i: Integer;
Buffer: String;
ListBoxItem : TListBoxItem;
ListBoxGroupHeader : TListBoxGroupHeader;
begin
ListBox1.BeginUpdate;
for c := 'a' to 'z' do
begin
// Add header ('A' to 'Z') to the List
ListBoxGroupHeader := TListBoxGroupHeader.Create(ListBox1);
ListBoxGroupHeader.Text := UpperCase(c);
ListBox1.AddObject(ListBoxGroupHeader);
// Add items ('a', 'aa', 'aaa', 'b', 'bb', 'bbb', 'c', ...) to the list
for i := 1 to 3 do
begin
// StringOfChar returns a string with a specified number of repeating characters.
Buffer := StringOfChar(c, i);
// Simply add item
// ListBox1.Items.Add(Buffer);
// or, you can add items by creating an instance of TListBoxItem by yourself
ListBoxItem := TListBoxItem.Create(ListBox1);
ListBoxItem.Text := Buffer;
// (aNone=0, aMore=1, aDetail=2, aCheckmark=3)
ListBoxItem.ItemData.Accessory := TListBoxItemData.TAccessory(i);
ListBox1.AddObject(ListBoxItem);
end;
end;
ListBox1.EndUpdate;
end;
这会产生(图片来自指定的 docwiki)
其他提示
这可以在FMX TLISTVIEW控制中轻松完成,同时使用Livebindings通过将TlistViewItem.header.Break字段连接到您要组的虚拟字段(它将是您的DB中的字段,在许多记录中是多余的)。
不隶属于 StackOverflow