简体   繁体   中英

Flex prevent item renderer recycling?

Is there a way to prevent recycling of item rendered with Spark components on Flex 4.5?

It might sound ridiculous, but actually this would do the trick until i understand a bit more the rendering life-cycle

First off, I do not recommend you do this . Item recycling is essential to any Flex app for performance reason. I suggest you take the time to understand instead of hacking away at trying to create the application.

But if you need to do it, you can always turn off virtualization in the layout:

<s:List>
   <s:layout>
      <s:VerticalLayout useVirtualLayout="false" />
   <s:/layout>
</s:list>

By default, virtualization is true.

Are you setting full state of item renderer with data? If you calculate state based on data, and forget to clear or reset it, you'll get buggy output because of recycling. For example:

public function set data(value:Object):void
{
     if (value.@blackBack == "true")
     {
          setStyle("backgroundColor", "black");
     }
}

When you see it first time, it will be seemingly OK - black items are black. But after you scroll list, you'll get reused items with black background everywhere. The solution is always set style to some value. Explain more about your problem, maybe it is the same?

I don't know if this helps, but before being introduced to ItemRenderers , we used to create n-number of custom component instances and call the addChild() method of a container to add them to screen. No recycling happens, all component instances are generated and added to the stage. Not such a good idea if you have a very large number of objects.

for(var i:int = 0; i < numComponents; i++)
{
    var customComponent:CustomComponent = new CustomComponent();
    // set properties to the customComponent, like data, width etc...
    vBox.addChild(customComponent);
}

Have the vBox as <mx:VBox id="vBox"/>

if you want to reset the elements, call vBox.removeAllChildren() and repeat the loop for adding them.

The drawback using this method, is you will have to manually remove all the elements and add them when the data is changed. And there is possibility of Memory leaks if some event listeners are not removed.

Hope this helps.

As far as I know,it is not possible no prevent the recycling of item renderers.

Regarding how item renderer recycling works,let me try to give a brief explanation:

Consider you have a list based component(like List,DataGrid etc) in your application.You are showing,lets say,100 rows of data(in any format you wish to).In most cases,due to screen space constraints and for better usability,you will not be showing 100 rows at the same time.Instead you will be showing,lets say 10 rows(using rowCount property or giving a specific height to the component),and use scrollbars to view the rest of the items.So,at any given time,of the 100 rows you have 10 rows visible and the rest 90 is offscreen.Keeping instances of the 90 offscreen rows' item renderers in memory,while you can view only 10 rows at a time is not a good perfomance practice.So what the flex framework does is that it will create the renderers for the visible rows(10 in this case) plus some 2 or 3 extra renderers(I am not sure how much exactly).So,here the flex framework creates only 13 item renderer instances.While scrolling,the framework reuses the already created item renderers to show the offscreen rows,by passing the data property of that particular row to the item renderer.In other words,the 14th row will reuse the item renderer created for first row,15th row will reuse 2nd row's,16 th will reuse 3rd's and so on,while scrolling down.

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM