Search code examples
delphifiremonkeydelphi-xe6

Grouping items inside TListView object?


I am trying to group the items inside TListView object, but I cant find the class responsible for grouping the objects, neither i were able to find such inside the documentation.

  • What is the class responsible about grouping items inside TListView object, and how to use it properly ?

The platform is Firemonkey ( Android/iOS) / Delphi XE6


Solution

  • The property I believe you're referring to is TListGroups, a collection that holds TListGroup items. There's a demo provided in the Delphi documentation.

    Unfortunately, it's only available in the VCL and not FMX, as the underlying functionality is part of the Windows ListView control that TListView wraps.

    The closest you can get in FMX is using TListBox and a TListBoxGroupHeader, which is covered in the Multi-Device Tutorial Using ListBox Components to Display a Table View (iOS and Android) in the docwiki:

    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;
    

    This produces (image from the indicated docwiki)

    Sample table view listbox appearance in iOS and Android