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

How to customize the 'itemsAdded' for a ListView.

Discussion in 'UI Toolkit' started by watsonsong, Sep 29, 2021.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I listen the 'itemsAdded' event on a ListView which bind to a SerializedObject. And I want to customize how to create a new element.
    I change the SerializedProperty and ApplyModifiedProperties inside the itemsAdded event callback, but actually there create two new element.
     
  2. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Actually I want to customize the ListView to support add 'SerializeReference' objects.
    So I want to customize the 'add' button, when the user click the add, it will show a menu, to choose which class want to create for this list.
    This is easy for ReorderableList, but no API for this ListView.
     
  3. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    Hello, I don't think I'm following what you're trying to do here. Do you not have an implementation of the makeItem on your ListView? Also if you're adding buttons to add items to your list you should add callbacks to your button that show your menu, etc - or did I miss something?
     
  4. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
  5. griendeau_unity

    griendeau_unity

    Unity Technologies

    Joined:
    Aug 25, 2020
    Posts:
    247
    If you want to rely on the ListView code to add the item to the source, you could probably use the
    itemsAdded
    or
    itemsSourceChanged
    callback. The list view will try to create a default item in the list and you can initialize it in the callback, so you could put the code to open your menu there.

    Otherwise, you could hijack the callback by querying the button and changing the clickable event on it by yours.
    Code (CSharp):
    1.  
    2. listView.Q<Button>("unity-list-view__add-button").clickable = new Clickable(() =>
    3. {
    4.       //your code.
    5. });
     
    Last edited: Oct 14, 2021
  6. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Thanks, I can query the button by 'unity-list-view__add-button', that what I am looking for.
     
  7. gregoired

    gregoired

    Joined:
    Apr 8, 2013
    Posts:
    20
    Hey ! I'm making a custom UIElement derived from ListView in order to implement a custom add button. From my Add button, I have an object reference. Now I need to update my serializedproperty array with this new value, and propagate the change to my UIElement. Is this something I can do right now or Am i missing a piece ?