Search Unity

Making a listview fill space

Discussion in 'UI Toolkit' started by Thundernerd, Nov 3, 2019.

  1. Thundernerd

    Thundernerd

    Joined:
    Jan 24, 2014
    Posts:
    20
    Hi there,

    I've been fiddling around with UIElements and I'm trying to create a settings provider using the new UIElements. However, no matter what I try I cannot seem to make the ListView fill the existing space.

    See the following example using the UI Builder


    As you can see in the UI Builder window there are two elements.
    The first element which is just a simple VisualElement has a margin of 5, padding of 10, and a 1px red border.
    The second element which is contained within the first element is a ListView and has a 1px green border.
    Both elements also have flex-grow set to 1.

    You can find the UXML over here
    https://gist.github.com/Thundernerd/90cce02cb45170e815862281ad68d280

    Hope anyone can enlighten me on how to make the ListView expand and take all available space.
     
    AdamBebko likes this.
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    This seems like an issue with the Settings window. It's own container is not set to stretch so, while your elements try to take up all the space inside their parent, the parent settings window container is super tiny. You can verify this using the UIElements Debugger (Window > Analysis > UIElements Debugger).
     
    mengkaima likes this.
  3. Thundernerd

    Thundernerd

    Joined:
    Jan 24, 2014
    Posts:
    20
    Yes thank you! I was about to write the same after finding out that the TemplateContainer in the Settings Window does not have flex-grow set to 1.
    I worked around this by adding a class to the TemplateContainer that enforces this.
     
  4. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    You can also use the `myVisualTreeAsset.CloneTree(root)` overload of CloneTree() to instantiate the contents of a UXML template directly inside an element (called "root" in this example), without creating a TemplateContainer.
     
  5. JonTerp

    JonTerp

    Joined:
    Aug 20, 2014
    Posts:
    9
    Just chiming in to say this bug hit me hard as well until I found this post.

    Using UIBuilder I placed a listview that would be invisible due to height being 0 for the reason you explained a bug. This resulted in hours of troubleshooting until I stumbled upon your answer. Please make this functionality more obvious as all the examples use `var newVisualElement = myVisualTreeAsset.CloneTree()`. It'll save other devs a lot of annoyance and trouble when adopting UIElements.
     
  6. AdamBebko

    AdamBebko

    Joined:
    Apr 8, 2016
    Posts:
    168
    Same here. I can't get my list view to stretch to fill any space.

    UI Debugger is just showing blank everything, so that doesn't help.

    Screen Shot 2021-07-06 at 1.19.34 PM.png

    I don't understand why but your fix of adding it to root directly worked, Thanks!

    But this is very non-obvious. that's 4 hours of my life I'll never get back. Perhaps this can be improved on in a future update.
     
  7. mengkaima

    mengkaima

    Joined:
    Apr 27, 2021
    Posts:
    8
    OMG you are really a lifesaver!!!