我对该LabelItemRenderer
类进行了子类化,以便为我的移动应用程序中的火花列表创建一个可扩展的渲染器。
当用户选择一个项目时,渲染器的大小会增加,并显示额外的数据。渲染器基本上是这样的(我这里去掉了无关紧要的部分,所以这基本上是伪代码)。
public class PositionsGridRenderer extends LabelItemRenderer
{
public function PositionsGridRenderer() {
super();
addEventListener(MouseEvent.CLICK, expandHandler);
}
protected override function createChildren():void {
super.createChildren();
_dg = new DataGroup();
_dg.visible = false;
addChild(_dg);
}
private function expandHandler(event:Event):void {
if(_gridVisible) {
if(!_detailClicked) {
_dg.visible = false;
_gridVisible = false;
}
_detailClicked = false;
} else {
_dg.visible = true;
_gridVisible = true;
}
}
public override function set data(value:Object):void {
if(!value) return;
super.data = value;
var pos:Position = data as Position;
label = pos.positionName;
_dg.dataProvider = pos.positionSymbols;
}
protected override function measure():void {
!_gridVisible ? measuredHeight = 30 : measuredHeight = 30 + getElementPreferredHeight(_dg);
this.height = measuredHeight;
}
protected override function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void {
setElementSize(labelDisplay, unscaledWidth, 30);
setElementPosition(labelDisplay, 10,10);
if(_gridVisible) {
setElementSize(_dg, unscaledWidth, getElementPreferredHeight(_dg));
setElementPosition(_dg, 0, 30);
} else {
setElementSize(_dg, unscaledWidth, 0);
}
invalidateSize();
}
}
}
按预期工作,我只是想知道是否有办法减少这个渲染器在我扩展它时所做的验证调用量。如果点击展开,则layoutContents
和measure
函数都会按以下顺序调用3次:layoutcontents -> measure, layoutcontens -> measure, layoutcontents -> measure。
我会理解它们被调用一次,因为我使大小无效,但三次似乎很奇怪。有谁知道为什么会发生这种情况,或者甚至可能如何防止这种情况发生?