Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How show show a simple list?

Discussion in 'UI Toolkit' started by MrMatthias, May 27, 2020.

  1. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    How can i show a simple list view without that size element and element Labels?
     
    Last edited: May 27, 2020
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    396
    From 2020.1 onwards, you can use
    listView.showBoundCollectionSize = false;
    to hide the first size element of the list.

    By default, the ListView's SerializedObject binding will create PropertyFields which have labels. If you create your own makeItem delegate, you can control how things will be displayed:
    Code (CSharp):
    1. listView.makeItem = ()  =>
    2. {
    3.     var container = new VisualElement();
    4.     container.Add( new TextField(){binding-path = "Name"});
    5.     container.Add( new TextField(){binding-path = "Test"});
    6. }
    You can set the TextField.label or change TextFields to PropertyFields in the above example to have the labels on the left.
     
    fxlange and MrMatthias like this.
  3. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    Mhm this gives me a warning that it can't bind Array.size, so implemented the bindItem callback:
    Code (CSharp):
    1.             var itemArrayProperty = serializedObject.FindProperty("_items");
    2.  
    3.             list.makeItem += () =>
    4.             {
    5.                 var entry = _entryTree.CloneTree();
    6.                 return entry;
    7.             };
    8.  
    9.             list.bindItem += (element, i) =>
    10.             {
    11.                 if (i == 0)
    12.                 {
    13.                     element.Clear();
    14.                     var label = new Label("This is just a label");
    15.                     element.Add(label);
    16.                     return;
    17.                 }
    18.            
    19.                 var p = itemArrayProperty.GetArrayElementAtIndex(index: i-1);
    20.                 var entry = element as TemplateContainer;
    21.                 entry.BindProperty(p);
    22.  
    23.                 var deleteButton = entry.Q<Button>("DeleteButton");
    24.                 deleteButton.clicked += () =>
    25.                 {
    26.                     ((ItemManager) target).RemoveItem(i-1);
    27.                     EditorUtility.SetDirty(target);
    28.                 };
    29.             };

    Is there a way to hide it this way? Setting its style to hidden just leaves an empty space, without the items moving up. Moving it to the end also effects the list height.
    Is it possible to have different heights for the items? (no)
     
    Last edited: May 28, 2020
  4. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    396
    Listview items need to be the same height. In your case, in your bindItem, you have to ensure that you can handle the visualElements in any previous state. By clearing elements and replacing with a label, this will cause problems when scrolling down, since this element will be recycled and bound to another index later.

    If you don't need to display really long lists of elements, maybe using a ScrollView directly would do.
     
    Last edited: May 28, 2020