1

In Flex 4 is it allowed to use states in Spark MXML item renderers?

I am asking this, because there are already "builtin" states like

<s:states> 
    <s:State name="normal"/> 
    <s:State name="hovered"/> 
</s:states> 

Can I add my own custom states, like "ingame" and "inlobby"?

Should I still write out the "normal" and "hovered" even if my AS3 code in the MXML item renderer doesn't use/need them?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416

1 Answers1

3

In Flex 4 is it allowed to use states in Spark MXML item renderers?

Yes! A renderer is just a component like any other; and you can add states if you wish.

I'm not sure how a Flex List handles setting the state of the renderers. So, it seems possible that the states in your renderer may get changed from your "Custom" state to one of the Flex states. You may have to override the currentState variable to prevent this sort of change.

Should I still write out the "normal" and "hovered" even if my AS3 code in the MXML item renderer doesn't use/need them?

No, don't add them if you don't need them. When a mouse interaction (like hovering) occurs, ItemRenderer will end up calling its getCurrentRendererState() method to decide what state to set on the renderer. In that method there are many statements like this:

if (hovered && hasState("hovered"))
    return "hovered";

If nothing matches, it will leave the renderer's state unchanged.

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • Yes, that is what I'm worried about: that hovering a mouse over the item will change the state from my custom "ingame" to "hovering"... – Alexander Farber Jul 03 '13 at 19:31
  • You'll have to try it and see. If you're own component does not implement a "hover" or "normal" state then there should be no issue. [I think] – JeffryHouser Jul 03 '13 at 19:38
  • 2
    @AlexanderFarber I edited @Reboog711's (hope you don't mind!) answer to address your other question. This will also give you insight on how it it will interfere w/your own states. Override `getCurrentRendererState()` if you need to work around it. – Sunil D. Jul 03 '13 at 19:51
  • 3
    @AlexanderFarber When an item renderer becomes this complex, I tend to encapsulate it in a seperate component, which is then composed into an ItemRenderer. This leaves the custom states to the custom component and the renderer states to the renderer (if need be), which seems cleaner to me. Though it is technically perfectly possible to handle it all inside the ItemRenderer alone as Reboog711 and Sunil D. pointed out. – RIAstar Jul 03 '13 at 22:05