Group Items in Delphi's TListView Control

Delphi 11.3 FMX 多设备平台中使用 TGrid 实现类似 TDBGrid 的效果 在界面里面放入 TStringGrid 控件 StringGrid1,TBindSourceDB 控件 BindSourceDB1,TBindingsList 控件 BindingsList1 ,TButton 控件 Button1。Delphi Firemonkey 中 TDBGrid 这个控件已经没有了。如何实现类似这个效果呢。其实可以用TGrid 来实现。查询里面用到的 connection 和 query 等控件那些一般的数据库用法,就不做过多描述了。对应的 query控件名称。 阅读详情

 By Zarko Gajic,

 

The TListView Delphi control displays and manages a list of items, displayed in columns, vertically or horizontally, with small or large icons.

Windows XP, and later versions, support grouping items in a list view. With the grouping feature of the ListView control, related sets of items can be displayed in groups. Groups are separated on the screen by horizontal group headers that contain the group titles.

Up until Delphi 2009, groups are not directly supported by the implementation of the TListView control.

 

Grouping Items in a TListView


If you are using Delphi 2007, Delphi 7 or any other previous version, you can also have grouping functionality in the TListView control.
The idea is to mimic item grouping with expandable and collapsible group items.

We'll hijack the Data property of the TListItem object. Items in a list view are presented by a TListItem object - used to specify the caption of the item, its glyph and other visual (and behavior) elements. The Data property specifies any application-specific data associated with the list item.

Note that the grouping implementation presented here leaves enough space for customization for your application-specific needs.

To follow along, have a ListView control on a form displaying its items in details view (ViewSytle = vsReport).

 

For the sake of simplicity we'll have two columns ("title" and "value") in the list view.
Download the sample project

 

Each list item will be assigned with a custom object, either a TGroupItem or a TItem. The TGroup item holds a group of TItem items. Group items can be expanded or collapsed to show or hide their "sub-items".

Normal list view items will be described with a TItem class:

TItem = class
private
  fTitle: string;
  fValue: string;
public
  constructor Create( const title, value : string) ;
   property Title: string read fTitle;
   property Value : string read fValue;
end;

An item that will be used to hold a collection of items is described by the TGroupItem class:

TGroupItem = class
private
  fItems : TObjectList;
  fCaption: string;
  fListItem: TListItem;
  fExpanded: boolean;
   function GetItems: TObjectList;
public
   constructor Create( const caption : string; const numberOfSubItems : integer) ;
  destructor Destroy; override;

   procedure Expand;
   procedure Collapse;

   property Expanded : boolean read fExpanded;
   property Caption : string read fCaption;
   property Items : TObjectList read GetItems;
   property ListItem : TListItem read fListItem write fListItem;
end;

The FillListViewGroups adds items to the list view:

procedure TForm1.FillListViewGroups;

   procedure AddGroupItem(gi : TGroupItem) ;
   var
    li : TListItem;
   begin
    li := lvGroups.Items.Add;

    li.Caption := gi.Caption;
    li.ImageIndex := 1; //collapsed

    li.Data := gi;
    gi.ListItem := li; //link "back"
   end;
begin
  ClearListViewGroups;

  AddGroupItem(TGroupItem.Create('Group A', 3)) ;
  AddGroupItem(TGroupItem.Create('Group B', 1)) ;
  AddGroupItem(TGroupItem.Create('Group C', 4)) ;
  AddGroupItem(TGroupItem.Create('Group D', 5)) ;
end;

A list item is added to the list view with its Data property pointing to an instance of a TGroupItem.

In the constructor we add dummy sub-items:

constructor TGroupItem.Create( const caption: string; const numberOfSubItems : integer) ;
var
  cnt : integer;
begin
  fCaption := caption;

   for cnt := 1 to numberOfSubItems do
   begin
    Items.Add(TItem.Create(caption + ' item ' + IntToStr(cnt), IntToStr(cnt))) ;
   end;
end;

Note that each TItem object added to the Items property of the TGroupItem will be presented as a normal list view item in the list view. Next, we need the functionality to expand or collapse groups. The OnDblClick event of the TListView control is used to handle this:

//handles TListView OnDblClick event
procedure TForm1.lvGroups DblClick(Sender: TObject) ;
var
  hts : THitTests;
  gi : TGroupItem;
begin
  hts := lvGroups.GetHitTestInfoAt(lvGroups.ScreenToClient(Mouse.CursorPos).X, lvGroups.ScreenToClient(Mouse.CursorPos).y) ;

   if (lvGroups.Selected <> nil) then
   begin
     if TObject(lvGroups.Selected.Data) is (TGroupItem) then
     begin
      gi := TGroupItem(lvGroups.Selected.Data) ;

       if NOT gi.Expanded then
        gi.Expand
       else
        gi.Collapse;
     end;
   end;
end;

The selected (double clicked) list view item is determined using the GetHitTestInfoAt method, if the list item is a "group item" the Expand or Collapse method is called.

Expand method will display sub-items, collapse will "hide" sub-items.

Here's the Expand method:

procedure TGroupItem.Expand;
var
  cnt : integer;
  item : TItem;
begin
  if Expanded then Exit;

  ListItem.ImageIndex := 0;
  fExpanded := true;

  for cnt := 0 to -1 + Items.Count do
  begin
    item := TItem(Items[cnt]) ;
    with TListView(ListItem.ListView).Items.Insert(1 + cnt + ListItem.Index) do
    begin
      Caption := item.Title;
      SubItems.Add(item.Value) ;
      Data := item;
      ImageIndex := -1;
    end;
  end;
end

Delphi TMS_FMX_UI_Pack_v3.7.7.5:完整的跨平台UI开发工具包 高效UI设计的核心在于优化用户的工作效率和体验,而不仅仅是外表上的吸引力。这需要设计师深入理解用户需求,运用适当的设计原则,并确保设计结果具有高度的可用性和良好的可访问性。Delphi的集成开发环境(IDE)是支持高效UI设计的基石,它提供了一套完整的工具集,使开发人员能够迅速地从概念转移到代码实现。Delphi IDE的核心特性包括:代码编辑器:它是一个强大的文本编辑器,集成了语法高亮、代码折叠、智能代码补全等特性,提高了代码编写效率。调试器。 阅读详情

相关推荐

Delphi 12 FMXUI,ListView很好用哦

Delphi 12 FMXUI,ListView很好用哦

Delphi TListview自绘实现每行或每列或每个单元格根据内容显示不同颜色

【代码】Delphi TListview自绘实现每行或每列或每个单元格根据内容显示不同颜色。

wh445306 1580

delphi_listview列表模式

delphi_listview列表模式

[Delphi] FMXUI - ListView用法简介

在 FMXUI 中,有 TListExView 和 TListViewEx 两个ListView。其中第一个是对Delphi原有TListView的功能扩展版,用法和原有的基本一样。TListViewEx 则是 FMXUI 原创的一个 ListView , 今天我们要介绍的就是它了。 一、 IListAdapter 数据适配器 TListViewEx 的设计思想与Java原生安卓开发类似...

weixin_33918114的博客 924

Delphi FMX跨平台框架

传统老Delphi人员大部分基本都是C/S端(客户端)开发上手(基于Windows开发),而FMX是Delphi中用于创建跨平台图形用户界面的框架。它允许开发人员使用单个代码库创建适用于多个操作系统的应用程序,如Windows、macOS、iOS和Android。FMX提供了丰富的界面控件和视觉效果,同时还支持触摸输入和多点触控等现代特性。开发人员可以快速轻松地构建漂亮、响应式的跨平台应用程序。最核心部分跨平台,一套代码同条件编译来解决实现不同平台下相关业务功能;

qq_37265818的博客 2365

ListView 分组显示信息

继上一篇ListView 显示柱状图,并解决滚动时内容重复的问题之后,针对于有分组的数据,应该如何处理呢?数据结构在第一篇文章中请查看NameValue类中的getNameValues()方法,里面放有name,value,filter。使用filter字段对数据结构进行分组 思路: 1.      拿到这样的一个数据结构,想到的第一件事,就是将数据进行重组,然后在使用Adapter绑定到L

rf001的专栏 1104

FMX的TListBox单选列表框

TListBox功能比较全,对于选择项,有“两种”模式,一种就是ListItem选中(界面上就是焦点和颜色变化),可以无,单选和多选。实际上,通过DefaultItemStyles.ItemStyle可以改变ListItem的许多样式,比如字体,颜色等等。需要一种类似TRadioBox的单选列表框。如果Item项是固定的,可以直接在TRectangle里放置TRadioBox来实现,界面还灵活。如果不考虑界面的CheckBox的方框,这样就可以了。但是如果Item项是动态的,用拼凑的实现就不灵活了。

舞天涯的专栏 561

winform ListView 扩展:1.失去焦点后保持选中行高亮;2.分组折叠

失去焦点后保持选中行高亮: public class ListViewEx : ListView { public ListViewEx() { GotFocus += new EventHandler(listView1_GotFocus); LostFocus += new EventHandler(

阿杰 5413

ExpandableListView可折叠列表

listView的一个子组件,可折叠列表。例如QQ联系人列表

lhp15575865420的博客 383

listview 分组显示+可展开分组+每个item可左右滑动

listview 分组显示+可展开分组+每个item可左右滑动+多级分组

好例子网_Listview 数据项分组显示.rar

好例子网_Listview 数据项分组显示

Delphi for iOS开发指南(9):在iOS应用程序中使用ListBox组件来显示TableView

Delphi for iOS开发指南(9):在iOS应用程序中使用ListBox组件来显示TableView         在FireMonkey iOS应用程序中使用ListBox组件来显示TableView   在iOS平台上,FireMonkey使用FMX.ListBox.TListBox组件来显示一个iOS Style的TableView,像这种ListBox:

DelphiTeacher的专栏 5304

listview分组实现、性能优化及错位解决

listview是Android开发中最常见的组件之一。在项目开发中,经常需要将listview分组。关于listview分组的原理可以参考android之listview分组实现,简单说就是:将分组的内容和分组的标题按顺序统一放在一个list中,根据list中的内容决定展示分组标题还是分组内容。本文主要讨论分组listview优化的问题,第一部分“listview分组实现”只做一个简单介绍,代码详

The day is another day 的博客 523

Delphi ListView快速操作通用设计与实现

Delphi ListView快速操作通用实现 作者:成晓旭 众所周知,Delphi ListView类直接进行Add、Update、Delete操作的速度是比较慢的,尤其是当数据量较大时,比如数据量达到5000、10000、50000时,速度真是可以说是“慢得惊人”。其实快速操作的方法非常简单,就当大家都知道了。在本人的工作中,很多项目都用到ListView,并且对速度的响应

IT旁观者 8281

DelphiTListView控件使用

要点: 1.代码备注的超级详细,如下: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TFor...

weixin_30458043的博客 313

Delphi ListView基本用法大全

 //增加项或列(字段)ListView1.Clear;ListView1.Columns.Clear;ListView1.Columns.Add;ListView1.Columns.Add;ListView1.Columns.Add;ListView1.Columns.Items[0].Caption:=id;ListView1.Columns.Items[1].Caption:=

jacknes009的专栏 2282
上一篇: Changing the Title of a Print Dialog in Delphi
下一篇: Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX, TXT
iseekcode
博客等级 码龄18年 36粉丝 14原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值