Documentation

Unity Engine

Download Unity

Unity Engine

ListView

A ListView is a vertically scrollable area that links to, and displays, a list of items.
Read time 15 minutesLast updated 2 days ago

Definition

[Icon("UIToolkit/Icons/ListView.png")][UxmlElement(libraryPath = "Containers")]public class ListView : BaseListView, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle, IBindable, ISerializationCallbackReceiver

Remarks

A ListView is a ScrollView with additional logic to display a list of vertically-arranged VisualElements. Each VisualElement in the list is bound to a corresponding element in a data-source list. The data-source list can contain elements of any type.
The logic required to create VisualElements, and to bind them to or unbind them from the data source, varies depending on the intended result. For the ListView to function correctly, you must supply at least a value for itemsSource.
It's also recommended to supply the following properties for more complex items:
The
ListView
creates multiple VisualElement objects for the visible items. As the user scrolls, the ListView recycles these objects and re-binds them to new data items.
To set the height of a single item in pixels, set the
item-height
property in UXML or the BaseVerticalCollectionView.itemHeight property in C# to the desired value.
To display a border around the scrollable area, set the
show-border
property in UXML or the BaseVerticalCollectionView.showBorder property in C# to
true
.
By default, the user can select one element in the list at a time. To change the default selection use the
selection-type
property in UXML or theBaseVerticalCollectionView.selectionType property in C#. To allow the user to select more than one element simultaneously, set the property to
Selection.Multiple
. To prevent the user from selecting items, set the property to
Selection.None
.
By default, all rows in the ListView have same background color. To make the row background colors alternate, set the
show-alternating-row-backgrounds
property in UXML or the BaseVerticalCollectionView.showAlternatingRowBackgrounds property in C# to AlternatingRowBackground.ContentOnly or AlternatingRowBackground.All. For details, see AlternatingRowBackground.
By default, the user can't reorder the list's elements. To allow the user to drag the elements to reorder them, set the
reorderable
property in UXML or the BaseVerticalCollectionView.reorderable property in C# to
true
.
To make the first item in the ListView display the number of items in the list, set the
show-bound-collection-size
property in UXML or the BaseListView.showBoundCollectionSize to true. This is useful for debugging. By default, the ListView's scroller element only scrolls vertically.
To enable horizontal scrolling when the displayed element is wider than the visible area, set the
horizontal-scrolling-enabled
property in UXML or the BaseVerticalCollectionView.horizontalScrollingEnabled to
true
.
For more information, refer to ListView.
For the difference between IDs and indices, refer to BaseVerticalCollectionView.
The following example creates an editor window with a list view of a thousand items.

Constructors

Constructor

Description

ListView()Creates a ListView with all default properties. The BaseVerticalCollectionView.itemsSource must all be set for the ListView to function properly.
ListView(System.Collections.IList,System.Single,System.Func{UnityEngine.UIElements.VisualElement},System.Action{UnityEngine.UIElements.VisualElement,System.Int32})Constructs a ListView, with all important properties provided.

Properties

Property

Description

bindItemCallback for binding a data item to the visual element.
destroyItemCallback invoked when a VisualElement created via ListView.makeItem is no longer needed and will be destroyed.
itemTemplateA UXML template that constructs each recycled and rebound element within the list. This template is designed to replace the ListView.makeItem definition.
makeItemCallback for constructing the VisualElement that is the template for each recycled and re-bound element in the list.
unbindItemCallback for unbinding a data item from the VisualElement.

Inheritance

Inherited Members