是否可以在不刷新整个dataProvider的情况下刷新单个itemRenderer?

时间:2013-11-14 08:06:27

标签: actionscript-3 flex actionscript flex4

问题在于:我已经获得了包含一些dataProvider和itemRendererFunction的列表。我不得不决定使用itemRenderFunction,因为我们需要为dataProvider中的不同项目提供不同的渲染器。问题是当dataProvider中的单个项目发生变化时,我必须刷新整个dataProvider,这在大量数据的情况下是非常有问题的。在这种情况下,有没有办法刷新单个项目?

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application name="list_test"
               xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               click="onApplicationClick(event)"
               >

    <fx:Script>
        <![CDATA[
            import mx.core.ClassFactory;

            import renderers.*;

            import spark.skins.spark.DefaultItemRenderer;

            private function itemRendererFunction(item:Object):ClassFactory
            {
                var rendererClass:Class = DefaultItemRenderer;
                switch (item.type)
                {
                    case "typeA":
                        rendererClass = ItemRendererTypeA;
                        break;
                    case "typeB":
                        rendererClass = ItemRendererTypeB;
                        break;
                    default:
                        break;
                }
                return new ClassFactory(rendererClass);
            }

            protected function onApplicationClick(event:MouseEvent):void
            {
                var item:Object = dp.getItemAt(0);
                item.type = "typeB";
                dp.itemUpdated(item);
                dp.refresh();
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:ArrayCollection id="dp">
            <fx:Object name="Item 1"
                       type="typeA"
                       />
            <fx:Object name="Item 2"
                       type="typeA"
                       />
            <fx:Object name="Item 3"
                       type="typeB"
                       />
        </s:ArrayCollection>
    </fx:Declarations>
            <s:List id="list"
                    labelField="name"
                    itemRendererFunction="itemRendererFunction"
                    horizontalCenter="0"
                    verticalCenter="0"
                    useVirtualLayout="true"
                    height="300"
                    dataProvider="{dp}"
                    >
            </s:List>
</s:Application>

我在项目渲染器类中的addToStage事件处理程序上放置了一些跟踪。每次点击都会调用它们。

1 个答案:

答案 0 :(得分:2)

this SO post中所述,您将希望避免在每次调用itemRendererFunction时返回ClassFactory的新实例:

    private var itemRendererTypeA:ClassFactory = new ClassFactory(ItemRendererTypeA);
    private var itemRendererTypeB:ClassFactory = new ClassFactory(ItemRendererTypeB);

    private function itemRendererFunction(item:Object):ClassFactory
    {
        switch (item.type)
        {
            case "typeA":
                return itemRendererTypeA;
                break;
            case "typeB":
                return itemRendererTypeB;
                break;
            default:
                break;
        }
    }