FMX.ListViewVariableHeightItems Sample

From RAD Studio Code Examples
Jump to: navigation, search

This demo shows how to calculate the dimensions of the text and how to dynamically adjust item height in a TListView so that the text fits in it.

Location

You can find the ListViewVariableHeightItems Sample project at:

  • Start | Programs | Embarcadero RAD Studio Alexandria | Samples and navigate to:
    • Object Pascal\Multi-Device Samples\User Interface\ListView\VariableHeightItems
  • Subversion Repository:
    • You can find Delphi code samples in GitHub Repositories. Search by name into the sample repositories according to your RAD Studio version.

Description

When ListView items are created, the drawable objects that comprise the visual presentation of the content are not created immediately. This usually happens when the ListView is being painted for the first time.

Appearances specify common views and properties for all items. However, the drawable objects that form the views should be manipulated directly to modify individual items. Items can be accessed at any time, but it is recommended to avoid manipulating them before the Appearance completes its job because all changes made before that will be overwritten.

The event TListView.OnUpdateObjects allows you to access drawable objects at the correct time. Each item is called for this event after their view has been updated. This sample includes a handler for TListView.OnUpdateObjects and calculates item dimensions based on the available item’s text width and length.

Note: To liven up the demo's visual aspect, the font's size and weight are also altered in the TListView.OnUpdateObjects handler.

How to Use the Sample

  1. Navigate to the one of the locations given above, and open:
    • Delphi: ListViewVariableHeightItems.dproj
    • C++: ListViewVariableHeightItems.cbproj
  2. Press F9 or choose Run > Run.

Implementation

This is a Variable Row Height demo. The procedure TVariableHeight.Button1Click creates the item with a random text length.

The GetTextHeight function calculates the height for the drawable text and the DefaultTextLayout.Create creates a TTextLayout to measure text dimensions and initialize the parameters with those of the drawable.

Drawable.TagFloat randomizes the font when updating for the first time.

Note: The demo uses DynamicAppearance for the items, but the same approach can be used using the classic appearances, predefined or user-defined.

Uses

See Also