0

我有用于列表的自定义 LabelItemRenderer,并覆盖集数据功能。在每个数据中,我都有属性“销售”,可以是真假。取决于此属性,我m adding icon. Everything seems fine until i将向上滚动列表几次。对于设置为 false 的列表索引,销售额为 true。我检查了我的数据提供者并找不到错误,但是在我的自定义项目中呈现数据是错误的并且图标显示在错误的位置。

这是我的代码:

package components {

import flash.display.Bitmap;

import spark.components.LabelItemRenderer;
import spark.components.supportClasses.StyleableTextField;
import spark.primitives.BitmapImage;

public class StoresList extends LabelItemRenderer {

    private var storeName:StyleableTextField;
    private var floor:StyleableTextField;
    private var mapListIco:Bitmap;
    private var promotions:Bitmap;
    private var sales:Boolean;
    private var compData:Object;

    public function StoresList() {

        super();
    }


    override public function set data(value:Object):void {

        compData = value;

        storeName.text = compData.title;
        floor.text = 'floor : ' + compData.floor;

        if (compData.sales && compData.salesIco) {
            promotions = compData.salesIco;
            addChild(promotions);
        }
    } 

    private function createStoreName():void {

        storeName = StyleableTextField(createInFontContext(StyleableTextField));
        storeName.editable = false;
        storeName.selectable = false;
        storeName.multiline = false;
        storeName.wordWrap = false;
        storeName.styleDeclaration = styleManager.getStyleDeclaration(".titleStyleName");

        addChild(storeName);
    }

    private function createFloortextField():void {

        floor = StyleableTextField(createInFontContext(StyleableTextField));
        floor.editable = false;
        floor.selectable = false;
        floor.multiline = false;
        floor.wordWrap = false;
        floor.styleDeclaration = styleManager.getStyleDeclaration(".floor");

        addChild(floor);
    }


    override protected function createChildren():void {

        // store name
        createStoreName();

        // floor
        createFloortextField();

        // maplist ico
        mapListIco = new Textures.mapListIco160;
        addChild(mapListIco);
    }


    override protected function measure():void {

        measuredWidth = getElementPreferredWidth(storeName) + getElementPreferredWidth(floor);
        // We only care about the "real" ascent
        measuredHeight = getElementPreferredHeight(storeName) +  getElementPreferredHeight(floor); 
    }


    override protected function drawBackground(unscaledWidth:Number, unscaledHeight:Number):void {

        super.drawBackground(unscaledWidth, unscaledHeight);
    }


    override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void {

        // store name
        var gap:Number = Math.round(unscaledHeight * .1);
        var storeNameHeight:Number = getElementPreferredHeight(storeName);
        var floorHeight:Number = getElementPreferredHeight(storeName);
        var storenameY:Number = Math.round( (unscaledHeight - (storeNameHeight + floorHeight + gap) ) * .5 );

        setElementSize(storeName, storeName.textWidth, storeName.textHeight);
        setElementPosition(storeName, unscaledWidth * .15, storenameY);

        // floor
        var floorY:Number = Math.round(storenameY + storeNameHeight);
        setElementPosition(floor, unscaledWidth * .15, floorY + gap);

        // map list
        setElementPosition(mapListIco, unscaledWidth - mapListIco.width - unscaledWidth * .05, (unscaledHeight - mapListIco.height) * .5);

        // promotions
        if (promotions) {

            setElementPosition(promotions, 
                (unscaledWidth * .15 - getElementPreferredWidth(promotions)) * .5, 
                (unscaledHeight - getElementPreferredHeight(promotions)) * .5)
        }
    }

}

}

4

1 回答 1

0

ItemRenderers 被重用 - 所以当渲染器被赋予新数据时[通过 set data()],你需要清除所有未使用的数据。在您的情况下,您需要显式隐藏图标(因为它可能在上次使用渲染器时可见)。例如:

override public function set data(value:Object):void {
    //always call super on an overriden function unless you have specific reason not to
    super.data = value;
    ...
    if (compData.sales && compData.salesIco) {
        promotions = compData.salesIco;
        promotions.visible = true;
    }
    else
    {
        promotions.visible = false;
    }
} 

我建议在 createChildren 而不是在集合数据中创建促销位图 - 因为重复添加和删除它是一项相对昂贵的操作。

于 2013-01-07T02:29:27.963 回答