解决egret引擎List虚拟布局不同高度Item对象重用时导致的布局异常问题

在实现聊天界面时,为优化性能使用了egret引擎的List并开启虚拟布局,但遇到不同高度Item对象重用导致的布局问题。文章详细描述了这个问题,并提出了自定义简化版List的解决办法,要求itemRenderer提供classname和getHEIGHT静态方法来确保正确显示。
摘要由CSDN通过智能技术生成

解决egret引擎List虚拟布局不同高度Item对象重用时导致的布局异常问题

功能需求:聊天界面,里面有文字聊天,红包等类型的消息,不同类型的Item高度是不一样的,需要通过对象重用来优化性能

问题描述:对象重用,很自然想到 开启eui.List的虚拟布局,设置useVirtualLayout=true,但是如果itemRenderer的height高度会变化的时候,egret引擎并不支持,运行时会出现item重叠等布局异常的问题

解决办法:自己重写一个简化版的list,scroller时对象重用和显示

核心代码

    class GameList extends eui.Group{
   
        public useVirtualLayout:boolean;

        public itemRenderer;

        private _itemRendererFunction:Function;
        public set itemRendererFunction(itemFun:any){
   
            this._itemRendererFunction = itemFun;
        }
        public get itemRendererFunction(){
   
            return this._itemRendererFunction;
        }

        private scroller:eui.Scroller;

        private desTop = 0;
        private margin = 16;
        private marginLeft = 0;
        private marginBottom = 0;

        private dataArr;
        private pool = {
   };
        private itemArr = [];

        private scrollTime;
        public constructor() {
   
            super();
        }

        public childrenCreated() {
   

            this.scroller = <eui.Scroller>this.parent;
            this.scroller = <eui.Scroller>this.parent.parent;

            this.scroller.addEventListener(egret.Event.CHANGE,this.onScrollEvent,this)
            this.scroller.addEventListener(eui.UIEvent.CHANGE_END,this.onScrollEndEvent,this)
        }

        private resetScrollV(scroller){
   

        }

        public set dataProvider(dp: eui.ArrayCollection){
   
            this.scroller.stopAnimation();//先停止滚动中的list

            // console.log("dataprovider")
            this.clean();
            this.setData(dp);

            var _dp = (<eui.ArrayCollection>dp);
            var type = eui.CollectionEvent.COLLECTION_CHANGE;
            if(!_dp.hasEventListener(type)) _dp.addEventListener(type, ()=>{
   
                if(!this.stage) return;
                // console.log("dataprovider2")

                this.setData(dp);

            }, this);
   
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星星之Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值