我为List定制了LabelItemRenderer,并覆盖了set data函数。在每个数据中,我都有属性'sales',可以是true或false。依赖于此属性,我m adding icon. Everything seems fine until i
将向上滚动列表几次。对于设置为false的列表索引,销售情况属实。我检查我的数据提供者并且找不到错误,但是在我的自定义项目中,渲染数据错误并且图标显示在错误的位置。
这是我的代码:
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)
}
}
}
}
答案 0 :(得分:0)
重复使用ItemRenderer - 因此当渲染器被赋予新数据[通过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而不是set数据中创建促销Bitmap - 因为重复添加和删除它是一项相对昂贵的操作。