/** * * @author * */ class ScrollList extends eui.DataGroup { public list = new eui.List(); public scroller: eui.Scroller; private noDataTxt:eui.Label; private noDataStr:string = "目前列表为空"; private noDataTxtColor = 0xffffff; public itemWidth:number = 0;//item宽度(包括间隔) public constructor() { super(); this.scroller = new eui.Scroller(); this.scroller.bounces = false; this.scroller.viewport = this.list; this.addChild(this.scroller); this.onResize(); this.list.addEventListener(eui.ItemTapEvent.ITEM_TAP, this.onItemTap, this); } public set dataProvider(dp: eui.ICollection){ this.scroller.stopAnimation();//先停止滚动中的list this.list.dataProvider = dp; this.addEventListener(egret.Event.ENTER_FRAME,this.onEnterFrame,this); if(this._addSourceListener){ var _dp = (dp); var type = eui.CollectionEvent.COLLECTION_CHANGE; if(!_dp.hasEventListener(type)) _dp.addEventListener(type, ()=>{ if(!this.stage) return; this.showData(); }, this); } } public get dataProvider(): eui.ICollection { return this.list.dataProvider; } public set itemRenderer(ir:any){ this.list.itemRenderer = ir; this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame,this); } public get itemRenderer(): any { return this.list.itemRenderer; } public set itemRendererSkinName(ir:any){ this.list.itemRendererSkinName = ir; } public get itemRendererSkinName(): any { return this.list.itemRendererSkinName; } public set selectedIndex(value:number){ this.list.selectedIndex = value; } public get selectedIndex(): number{ return this.list.selectedIndex; } public set selectedItem(value: any) { this.list.selectedItem = value; } public get selectedItem(): any { return this.list.selectedItem; } public set allowMultipleSelection(value: boolean) { this.list.allowMultipleSelection = value; } public get allowMultipleSelection(): boolean { return this.list.allowMultipleSelection; } public get selectedIndices(): any { return this.list.selectedIndices; } public set useVirtualLayout(value: boolean){ this.list.useVirtualLayout = value; } public get useVirtualLayout(): boolean { return this.list.useVirtualLayout; } public set layout(value: any) { this.list.layout = value; } public get layout(): any { return this.list.layout; } public setNoDataStr(str:string){ this.noDataStr = str;//传""表示不显示 } /* 业务界面 有时候对list只会设置1次dataProvider,然后通过改变其source来更新值,这样会导致无法修改noDataTxt.visible ,这里添加一个方法供外层显式调用一下,就会为ArrayCollection添加事件监听,有修改的时候会更新 showData 方法*/ private _addSourceListener:boolean = false; public addSourceListener(){ this._addSourceListener = true; } /*public dataProviderRefreshed():void{ this.list.dataProviderRefreshed(); }*/ public addEventListener(type: string,listener: Function,thisObject: any,useCapture?: boolean,priority?: number): void{ this.list.addEventListener(type, listener, thisObject, useCapture, priority); } public removeEventListener(type: string,listener: Function,thisObject: any,useCapture?: boolean,priority?: number): void{ this.list.removeEventListener(type, listener, thisObject, useCapture); } public once(type: string, listener: Function, thisObject: any, useCapture?: boolean, priority?: number): void{ this.list.once(type, listener,thisObject, useCapture, priority); } private onEnterFrame(){ this.removeEventListener(egret.Event.ENTER_FRAME,this.onEnterFrame,this); this.onResize(); this.showData(); } private showData(){ var dp = this.list.dataProvider; if(!dp) return; /*底层修正 数量不足一行时 采用两端对齐导致间距很大 不好看问题 方法是:不足4个,采用左对齐*/ if(this.list.layout instanceof eui.TileLayout && this.itemWidth>0){ var layout = this.list.layout; var itemNum = this.width/this.itemWidth; if(dp.length < itemNum && layout.columnAlign == eui.ColumnAlign.JUSTIFY_USING_WIDTH){ layout.columnAlign = eui.ColumnAlign.LEFT; } else if(dp.length >= itemNum && layout.columnAlign == eui.ColumnAlign.LEFT){ layout.columnAlign = eui.ColumnAlign.JUSTIFY_USING_WIDTH; } } var noDataTxt = this.noDataTxt; if(dp && dp.length == 0){ if(!noDataTxt){ noDataTxt = this.noDataTxt = new eui.Label(this.noDataStr); noDataTxt.size = 18; noDataTxt.textColor = this.noDataTxtColor; this.addChild(noDataTxt); noDataTxt.x = (this.width - noDataTxt.width)/2; noDataTxt.y = this.y + this.height/2 - 80; } } if(noDataTxt) noDataTxt.visible = dp.length == 0; } private onResize(){ this.scroller.height = this.height; this.scroller.width = this.width; } public setTxtColor(color){ this.noDataTxtColor = color; if(this.noDataTxt) this.noDataTxt.textColor = color; } private onItemTap(event: eui.ItemTapEvent) { this.dispatchEvent(event); //console.log(event) } public clear(){ this.list.dataProvider = null; //必现调用下面2句,并且 需要在hide之前调用 this.list.dataProviderRefreshed(); this.validateNow(); } }