ListView, TileView and Table

  • All collections support virtualization: gameobjects will be created only for visible items.
  • Add Selectable component to use keyboard and gamepad navigation.
  • Different ListView, TileView and Table can display the same list.

List Types

_images/ListView-fixed-size.png

ListView with Fixed Size

_images/ListView-variable-size.png

ListView with Variable Size

_images/TileView-fixed-size.png

TileView with Fixed Size

_images/TileView-variable-size.png

TileView with Variable Size

Get items

var items = listView.DataSource;

Set items

var items = new ObservableList<ListViewIconsItemDescription>();
listView.DataSource = items;

var items2 = new List<ListViewIconsItemDescription>();
listView.DataSource = items2.ToObservableList();

Display same list with ListView, TileView or Table

var items = new ObservableList<ListViewIconsItemDescription>();
listView.DataSource = items;
tileView.DataSource = items;
table.DataSource = items;

Get last selected index

Debug.Log(listView.SelectedIndex);

Get selected indices

var indices = listView.SelectedIndices;
Debug.Log(string.Join(", ", indices.ConvertAll(x => x.ToString()).ToArray()));

Last selected item

Debug.Log(listView.SelectedItem.Name);

Get selected items

var selected_items = listView.SelectedItems;
Debug.Log(string.Join(", ", selected_items.ConvertAll(x => x.Name).ToArray()));

Delete specified item

listView.DataSource.Remove(items[0]);

Delete item by index

listView.DataSource.RemoveAt(0);

Clear list

listView.DataSource.Clear();

Add item

var new_item = new ListViewIconsItemDescription()
{
   Icon = sampleIcon,
   Name = "test item",
};
listView.DataSource.Add(new_item);

Add items

var new_items = new List<ListViewIconsItemDescription>()
{
   new_item,
   new_item,
   new_item,
};
listView.DataSource.AddRange(new_items);

Optimization

// Use BeginUpdate() and EndUpdate() to keep widget from updating on each change.
// All changes after BeginUpdate() call will be displayed with EndUpdate() call.
var items = listView.DataSource;
items.BeginUpdate();

items.Clear();
items.Add(new_item);
items.Add(new_item);
items.Add(new_item);
items.AddRange(new_items);
items.RemoveAt(0);

// widget will be updated after EndUpdate() call
items.EndUpdate();

Replace item

listView.DataSource[0] = new ListViewIconsItemDescription()
{
   Name = "new item"
};

Sort

// Sort by LocalizedName or Name in ascending order
Comparison<ListViewIconsItemDescription> ItemsComparisonAsc = (x, y) => x.Name.CompareTo(y.Name);

// sort by LocalizedName or Name in descending order
Comparison<ListViewIconsItemDescription> ItemsComparisonDesc = (x, y) => -(x.Name).CompareTo(y.Name);

// sort items only once
items.Sort(ItemsComparisonAsc);

Enable permanent sort

items.Comparison = ItemsComparisonDesc;

Important

Items will be always sorted, but if you use .BeginUpdate() then items will be re-sorted only after .EndUpdate() call.

Disable permanent sort

items.Comparison = null;

Set selected index

listView.SelectedIndex = 1;

Or:

listView.Select(1);

Behavior is different if you enable MultipleSelect:

  • listView.SelectedIndex = 1 last selected item will be deselected and specified item will be selected.
  • listView.Select(1) new item will be added to selected items.

Deselect

listView.SelectedIndex = -1;

Or:

listView.Deselect(1);

Scroll to item

listView.ScrollToAnimated(index);