Unity 2022.3.5 - ListView first click doesn't trigger events

I’m working on a project and I’m having some problems to make the list views behave like I want.
I have different UI documents that I’m switching on and off like this:

public void Open(T_Data data = default)
  {
    docRoot.style.display = DisplayStyle.Flex;
    this.data = data;
    OnOpen();
  }
  public void Close()
  {
    InventoryElement.inventories = new List<InventoryElement>();
    OnClose();
    docRoot.style.display = DisplayStyle.None;
  }

And every time that I switch on a UI document with some list views in it I have this problem.
It doesn’t register the first click on an item of the list, but after that everything works perfectly until I close it.
The code that I use is divided in 2 blocks:

  • This one is called at the awake of a “Controller” code
protected void ListViewSetup(ListView list, VisualElement template, SelectionType selectionType,
    Action<IEnumerable<object>> onSelectionChange)
  {
    template.visible = true;
    list.makeItem = () => Clone(template, doc.visualTreeAsset);
    template.visible = false;
    list.fixedItemHeight = template.contentRect.height;
    list.selectionType = selectionType;
    list.selectionChanged += onSelectionChange;
  }
  • This one is called on the opening
protected void ListViewOnOpen<K>(ListView list, List<K> items, Action<VisualElement, int, List<K>> bind)
  {
    list.itemsSource = items;
    list.bindItem = (e, i) => { bind(e, i, items); };
    list.ClearSelection();
  }

Do you have some suggestions?

Hello. This looks like a bug to me. Could you report it so we can investigate? (Help → Report a Bug…)

As a workaround, have you tried to manually focus the recently opened docRoot?

Good morning, I tried to focus without success. I’ve solve the problem by adding some empty callback to some events.

I hope that it can help with the bug fixing

protected void ListViewSetup(ListView list, VisualElement template, SelectionType selectionType,
    Action<IEnumerable<object>> onSelectionChange)
  {
    template.visible = true;
    list.makeItem = () => Clone(template, doc.visualTreeAsset);
    template.visible = false;
    list.fixedItemHeight = template.contentRect.height;
    list.selectionType = selectionType;
    list.selectionChanged += onSelectionChange;

    //// Fix for listView
    list.onSelectionChange += (a) => { };
    list.onSelectedIndicesChange += (a) => { };
    list.onItemsChosen += (a) => { };
    list.selectionChanged += (a) => { };
    //// Fix for listView
  }
1 Like