Custom item renderers with TreeView
can be somewhat tricky, since they're pretty much required to be more complex than item renderers in other data containers like ListView
, GroupListView
, and GridView
due to the nature of hierarchical data. In times like this, it may be easier to use DisplayObjectRecycler.withClass()
. However, I think we can make it work with DisplayObjectRecycler.withFunction()
.
When a branch is toggled open or closed, that doesn't necessarily mean that the selectedItem
will change. Selection and open/close status are different.
With that in mind, instead of passing treeView.selectedItem
to toggleBranch()
, you should try to find a different way to get the item from the data provider, such as calling itemRendererToItem
.
The change to your code might be as simple as this:
var itemToToggle = treeView.itemRendererToItem(itemRenderer);
treeView.toggleBranch(itemToToggle, openedToggle.selected);