Egret语法DataGroup使用案例

class Main extends egret.DisplayObjectContainer{
    public constructor()
    {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
    }

    public onAddToStage(event: egret.Event): void
    {
        //先创建一个数组
        var sourceArr: any[] = [];
        for(var i: number = 1;i < 5;i++)
        {
            sourceArr.push({ label: "item" + i,num: "num=" + Math.ceil(Math.random() * 100)});
        }
        //用ArrayCollection包装
        var myCollection: eui.ArrayCollection = new eui.ArrayCollection(sourceArr);
        var dataGroup: eui.DataGroup = new eui.DataGroup();
        dataGroup.dataProvider = myCollection;
        dataGroup.percentWidth = 100;
        dataGroup.percentHeight = 100;
        dataGroup.useVirtualLayout = true;//性能优化,避免创建过多对象实例
        dataGroup.itemRenderer = LabelRenderer;
        this.addChild(dataGroup);
        
    }
}
//类LabelRenderer一定要继承eui.ItemRenderer
class LabelRenderer extends eui.ItemRenderer
{
    private labelDisplay: eui.Label;
    public constructor()
    {
        super();
        this.touchChildren = true;
        this.labelDisplay = new eui.Label();
        this.addChild(this.labelDisplay);
    }
    protected dataChanged(): void
    {
        this.labelDisplay.text = this.data.label + " "+ this.data.num;
    }
}

 



Bookmark the permalink.

Comments are closed.