4 Replies Latest reply on Sep 21, 2010 8:13 AM by gengwang

    Access itemRenderer in Spark List

    gengwang Level 1



      I've got a List with dynamically generated itemRenderers based on the item types (like a Manager has one kind of renderer and the other kind of employees has other types of renderers). I'd like to access the itemRenderer from the "selectedItem" handler of the List, but the selectedItem(item:Object) only has direct acess to the data item, not its renderer.


      I prefer to decouple the item from its renderer as much as possible.


      Sorry if this is considered a cross post as I've noticed a similar post at stackoverflow. But it didn't seem to get any answers so far. Thanks.

        • 1. Re: Access itemRenderer in Spark List
          jd_ice Level 1

          Access your dataprovider instead like so


          protected function myList_changingHandler(event:IndexChangeEvent):void


               var item:* = myList.dataProvider.getItemAt(event.newIndex);

               // do something here




          Best Regards!

          • 2. Re: Access itemRenderer in Spark List
            gengwang Level 1

            Hi, Thanks for the attention. I've digged around a bit but havn't found any satisfactory answer.


            Here is what I've found so far:


            1. In Flex, it's easy to access data item from its renderer but not the other way around , this includes spark List/SelectableList, ect. spark.components.IItemRendererOwner doesn't seem to provide any inferface to access its renderer. It has two public methods:

            updateRenderer(renderer:IVisualElement, itemIndex:int, data:Object):void

            2. Flash seems to be more flexible in this respect.For example, fl.controls.SelectableList provides this method:

            public function itemToCellRenderer(item:Object):ICellRenderer, which I can get the renderer from the data item.


            For now, I can either retreat to handle lower level click event and walk up the visual tree to get the right renderer (very messy), or in my data item i can have a dynamic field that stores a reference to its run-time renderer (not clean either:)). It makes sense that data should be oblivious to their renderers but it would be nice if the list controls, or as you mentioned, the dataProvider of the list controls could provide a way to get the renderers, esp. for the scenario if you have dynamically generated rendereres.


            Please let me know if I'm missing anything obvious. Very much appreciate it!

            • 3. Re: Access itemRenderer in Spark List
              Shongrunden Adobe Employee

              In Flex a data item does not directly map to a specific renderer.  This is because of virtual layout and item renderer recycling.  If the data item is not in view then it likely won't have a renderer associated with it and it's possible for a single data item to be pushed into different renderer instances over the course of its lifetime (for example when a List is being scrolled).  The best practice in spark is to change the data item and design your renderer to detect and react to those changes rather than interacting with the renderer instance directly.


              Check out the spark virtualization spec for more information on virtual layout and renderer recycling: http://opensource.adobe.com/wiki/display/flexsdk/Spark+Virtualization


              If you really do want to get access to the renderer instances you still can, but you should only do so after you have a really solid understanding of what's going on.


              Here is an example, you can use the getElementAt() method of the List's dataGroup skin part:

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                             xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="traceRenderers()">
                          private function traceRenderers():void {
                              for (var i:int = 0; i < list1.dataGroup.numElements; i++)
                  <s:List id="list1" height="60">
              • 4. Re: Access itemRenderer in Spark List
                gengwang Level 1

                Thanks for the lead. I am new to the visual element API (just heard of it from Adobe TV/Flex 360 yesterday:)