Search Unity

[SOLVED] Making a ListView refresh

Discussion in 'UI Toolkit' started by Fritsl, Aug 19, 2020.

  1. Fritsl

    Fritsl

    Joined:
    Mar 10, 2013
    Posts:
    211
    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?

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class ListViewTest : EditorWindow
    8. {
    9.     [MenuItem("Test/ListViewTest")]
    10.     public static void OpenDemoManual()
    11.     {
    12.         GetWindow<ListViewTest>().Show();
    13.     }
    14.     List<string> items;
    15.     ListView listView;
    16.  
    17.     public void OnEnable()
    18.     {
    19.         SetupNewData();
    20.         rootVisualElement.Add(listView);
    21.     }
    22.  
    23.     void SetupNewData()
    24.     {
    25.         const int itemCount = 10;
    26.         items = new List<string>(itemCount);
    27.         for (int i = 1; i <= itemCount; i++)
    28.             items.Add(i.ToString() + " " + UnityEngine.Random.value);
    29.  
    30.         SetupListView();
    31.     }
    32.     void SetupListView()
    33.     {
    34.         Func<VisualElement> makeItem = () => new Label();
    35.         Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items[i];
    36.         const int itemHeight = 16;
    37.         listView = new ListView(items, itemHeight, makeItem, bindItem);
    38.         listView.selectionType = SelectionType.Single;
    39.         listView.onSelectionChanged += objects => SetupNewData();
    40.         listView.style.flexGrow = 1.0f;
    41.  
    42.         Debug.Log("Now refreshing"); // This executes: When list is clicked, this is written in the console
    43.         listView.Refresh(); // This does not do anything, the list is not updated, data are changed, but no refresh in UI?!?
    44.     }
    45. }
     
  2. Fritsl

    Fritsl

    Joined:
    Mar 10, 2013
    Posts:
    211
    Oh, got it, the order was wrong, this works for demo, list updates with random numbers when clicked on:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class ListViewTest : EditorWindow
    8. {
    9.     [MenuItem("Test/ListViewTest")]
    10.     public static void OpenDemoManual()
    11.     {
    12.         GetWindow<ListViewTest>().Show();
    13.     }
    14.     List<string> items;
    15.     ListView listView;
    16.  
    17.     public void OnEnable()
    18.     {
    19.         SetupNewData();
    20.         rootVisualElement.Add(listView);
    21.     }
    22.  
    23.     void SetupNewData()
    24.     {
    25.         const int itemCount = 10;
    26.         items = new List<string>(itemCount);
    27.         for (int i = 1; i <= itemCount; i++)
    28.             items.Add(i.ToString() + " " + UnityEngine.Random.value);
    29.  
    30.         if (listView != null)
    31.         {
    32.             listView.Refresh();
    33.         }
    34.         else
    35.         {
    36.             SetupListView();
    37.         }
    38.     }
    39.     void SetupListView()
    40.     {
    41.         Func<VisualElement> makeItem = () => new Label();
    42.         Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items[i];
    43.         const int itemHeight = 16;
    44.         listView = new ListView(items, itemHeight, makeItem, bindItem);
    45.         listView.selectionType = SelectionType.Single;
    46.         listView.onSelectionChanged += objects => SetupNewData();
    47.         listView.style.flexGrow = 1.0f;
    48.     }
    49. }
    50.  
     
  3. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Hello,
    I'm doing something similar to your example, but in my case, the number of items is different on every call. And I'm getting a
    "ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection." exception.
    Any idea?
     
    berber_hunter likes this.
  4. berber_hunter

    berber_hunter

    Joined:
    Apr 30, 2021
    Posts:
    14
    Ever solve this?
     
    LinnPeder likes this.
  5. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    76
    I don`t know if we are experiencing the same problem, but for my case it was reassigning the same array to itemsSource of list view. No rebuild or refresh items needed, they did not work on their own anyway.