I understand that the ListView doesn’t support multiple heights yet , and that’s fine for me. I’ve got a ton of items (1k+) that I’d like to view, and they’re all drawing a uniform interface. I’d like to try to genericize the code as much as possible, though, especially if I later decide to update the UI for the items I’m drawing. To that end, I’d like to avoid hard-coding the itemHeight if at all possible.
Is it possible? I tried getting the current dimensions of the UI Element in the MakeItem function, but those values are all coming back as NaN. Once the ListView is created, the itemsSource is the actual list of things, not the list of UI Elements, right?
I was thinking I’d be able to create the ListView, then get the height, then set the itemHeight to that value, but I don’t know how to access one of the drawn items after the fact and the values are NaN when they’re created.
int nItems = 100;
var testItems= new List<TestUIItem>(nItems);
for(int i=0; i<nItems; i++)
{
testItems.Add(new TestUIItem(i));
}
int placeholderHeight = 16;
var testTemplate = Resources.Load<VisualTreeAsset>("Uxml/Test/TestUIItemView");
Func<VisualElement> makeItem = () => testTemplate.CloneTree();
Action<VisualElement, int> bindItem = (e, i) => e.Q<TestUIItemViewModel>("test-item").SetItem(testItems[i]);
ListView listView = new ListView(testItems, placeholderHeight, makeItem, bindItem);
// Get a child UIElement or VisualElement?
// Get the height of that child?
listView.itemHeight = childElementHeight;
Maybe I’m not using the right code for getting the height of a VisualElement, or I’m getting it at the wrong time or something. If I use CloneTree()
and get an instance of the UI then try to call testItem.resolvedStyle.minHeight.value
, I get zero, which is what I also get for testItem.resolvedStyle.maxHeight.value
. If I try testItem.resolvedStyle.height
I get NaN. If I try testItem.style.height
I get zero.
Again, this isn’t really the end of the world, but I’m fixating on this issue at the moment (procrastinating on other stuff, I’m sure), so any help would be appreciated.