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
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsControlsModule
- Inherits from: BaseListView
- Implements: IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle, IBindable, ISerializationCallbackReceiver
[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 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.
ListViewTo set the height of a single item in pixels, set the property in UXML or the BaseVerticalCollectionView.itemHeight property in C# to the desired value.
item-heightTo display a border around the scrollable area, set the property in UXML or the BaseVerticalCollectionView.showBorder property in C# to .
show-bordertrueBy default, the user can select one element in the list at a time. To change the default selection use the property in UXML or theBaseVerticalCollectionView.selectionType property in C#. To allow the user to select more than one element simultaneously, set the property to . To prevent the user from selecting items, set the property to .
selection-typeSelection.MultipleSelection.NoneBy default, all rows in the ListView have same background color. To make the row background colors alternate, set the property in UXML or the BaseVerticalCollectionView.showAlternatingRowBackgrounds property in C# to AlternatingRowBackground.ContentOnly or AlternatingRowBackground.All. For details, see AlternatingRowBackground.
show-alternating-row-backgroundsBy default, the user can't reorder the list's elements. To allow the user to drag the elements to reorder them, set the property in UXML or the BaseVerticalCollectionView.reorderable property in C# to .
reorderabletrueTo make the first item in the ListView display the number of items in the list, set the property in UXML or the BaseListView.showBoundCollectionSize to true. This is useful for debugging. By default, the ListView's scroller element only scrolls vertically.
show-bound-collection-sizeTo enable horizontal scrolling when the displayed element is wider than the visible area, set the property in UXML or the BaseVerticalCollectionView.horizontalScrollingEnabled to .
horizontal-scrolling-enabledtrueFor 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 |
|---|---|
| bindItem | Callback for binding a data item to the visual element. |
| destroyItem | Callback invoked when a VisualElement created via ListView.makeItem is no longer needed and will be destroyed. |
| itemTemplate | A UXML template that constructs each recycled and rebound element within the list. This template is designed to replace the ListView.makeItem definition. |
| makeItem | Callback for constructing the VisualElement that is the template for each recycled and re-bound element in the list. |
| unbindItem | Callback for unbinding a data item from the VisualElement. |