I cannot make a ListView refresh dynamically → Only if I Close/open window, or redraw entire UI, merely calling ListView.Refresh() does not appear to have any effect.
How would I go about and make a ListView Refresh/Redraw?
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class ListViewTest : EditorWindow
{
[MenuItem("Test/ListViewTest")]
public static void OpenDemoManual()
{
GetWindow<ListViewTest>().Show();
}
List<string> items;
ListView listView;
public void OnEnable()
{
SetupNewData();
rootVisualElement.Add(listView);
}
void SetupNewData()
{
const int itemCount = 10;
items = new List<string>(itemCount);
for (int i = 1; i <= itemCount; i++)
items.Add(i.ToString() + " " + UnityEngine.Random.value);
SetupListView();
}
void SetupListView()
{
Func<VisualElement> makeItem = () => new Label();
Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items[i];
const int itemHeight = 16;
listView = new ListView(items, itemHeight, makeItem, bindItem);
listView.selectionType = SelectionType.Single;
listView.onSelectionChanged += objects => SetupNewData();
listView.style.flexGrow = 1.0f;
Debug.Log("Now refreshing"); // This executes: When list is clicked, this is written in the console
listView.Refresh(); // This does not do anything, the list is not updated, data are changed, but no refresh in UI?!?
}
}