Egret之Tabbar通用页签
admin
2023-02-17 14:20:00
0

目标 : 实现Tabbar也签的通用。

一 : 美术资源的准备(自己做的 , 很挫 , 勿喷 )如下
Egret之Tabbar通用页签

二 : 通用页签ItemRenderer的皮肤 如下
Egret之Tabbar通用页签

三 :tabbar的测试UI皮肤 如下
Egret之Tabbar通用页签

四 :ItemRenderer的实现

module common{
    /**
     * 通用的页签CELL
     * @author Husz
     */
    export class common_TabBarCell extends eui.ItemRenderer{
        private img_yes : eui.Image = null;
        private img_no : eui.Image = null;
        private img_txt : eui.Image = null;

        public constructor(){
            super();
            this.skinName = "resource/common_skins/common_TabBarCell.exml";
        }
        protected createChildren():void{
            super.createChildren();
        }
        protected dataChanged() : void{
            let $dataModel : common_TabBarCell_Data = this.data;
            this.handlerBackGround( $dataModel );
            this.handlerTxt( $dataModel );
        }

        /**
         * 处理背景
         * @param {common.common_TabBarCell_Data} $dataModel
         */
        private handlerBackGround( $dataModel : common_TabBarCell_Data ) : void{
            let $selected : boolean = $dataModel._selected;
            this.img_yes.visible = $selected;
            this.img_no.visible = !$selected;
        }

        /**
         * 处理文本
         * @param {common.common_TabBarCell_Data} $dataModel
         */
        private handlerTxt( $dataModel : common_TabBarCell_Data ) : void{
            let $textPathRes : string = "";
            if($dataModel._selected == true){
                $textPathRes = $dataModel._yes_txt_res;
            }else{
                $textPathRes = $dataModel._no_txt_res;
            }
            this.img_txt.source = RES.getRes($textPathRes);
        }

        /**
         * 是否已经处于选择的状态(只读。。。。。。)
         * @returns {boolean}
         * @constructor
         */
        public get IsSelected() : boolean{
            let $dataModel : common_TabBarCell_Data = this.data;
            return $dataModel._selected;
        }
    }

    /**
     * 通用的页签CELL的数据模型
     * @author Husz
     */
    export interface common_TabBarCell_Data{
        /**数据的下标*/
        _index : number;
        /**是否处于选择状态*/
        _selected : boolean;
        /**选择状态下的美术文本资源*/
        _yes_txt_res : string;
        /**未选择状态下的美术文本资源*/
        _no_txt_res : string;
    }
}

五 : TabBarDemoView UIDemo实现

module app{
    import common_TabBarCell = common.common_TabBarCell;
    import common_TabBarCell_Data = common.common_TabBarCell_Data;
    /**
     * 此UI面板测试通用的Tabbar页签Cell
     * @author Husz
     */
    export class TabBarDemoView extends eui.Component implements eui.UIComponent{
        private bar_target : eui.TabBar = null;
        private con_0 : eui.Group = null;
        private con_1 : eui.Group = null;

        private _data2TabBar_arr : Array = null;
        private _cur_index2TabBar : number = 0;
        private _all_channel : Array = null;

        public constructor(){
            super();
            this.skinName = "resource/eui_skins/TabBarDemoSkin.exml";
        }

        protected childrenCreated():void{
            super.childrenCreated();
            this._all_channel = [
                this.con_0,
                this.con_1
            ];
            this.handlerListener2Tabber( true );
            this.initView2Tabbar();
        }

        private handlerListener2Tabber( $isAdd : boolean ) : void{
            if( $isAdd ){
                if( !this.bar_target.hasEventListener(eui.ItemTapEvent.ITEM_TAP) )
                    this.bar_target.addEventListener( eui.ItemTapEvent.ITEM_TAP , this.onTabbarItemClick , this  );
            }else{
                if( this.bar_target.hasEventListener(eui.ItemTapEvent.ITEM_TAP) )
                    this.bar_target.removeEventListener( eui.ItemTapEvent.ITEM_TAP , this.onTabbarItemClick , this  );
            }
        }

        /**
         * 处理页签切换
         * @param {eui.ItemTapEvent} $e
         */
        private onTabbarItemClick( $e : eui.ItemTapEvent ) : void{
            if( this._cur_index2TabBar != $e.itemIndex ){
                this._cur_index2TabBar = $e.itemIndex;
                let $dataCell : common_TabBarCell_Data = null;
                let $count : number = 0;
                for( let $i : number = 0 , $j :number = this._data2TabBar_arr.length ; $i < $j ; $i ++ ){
                    $dataCell = this._data2TabBar_arr[$i];
                    if( $dataCell._index == this._cur_index2TabBar ){
                        $dataCell._selected = true;
                        $count ++;
                    }else{
                        if( $dataCell._selected != false ){
                            $dataCell._selected = false;
                            $count ++;
                        }
                    }
                    if($count >= 2){
                        break;
                    }
                }
                //切换频道
                this.channelChange();
                this.bar_target.dataProvider = new eui.ArrayCollection( this._data2TabBar_arr );
            }
        }

        /**
         * 初始化页签数据
         */
        private initData2Tabbar() : void{
            this._data2TabBar_arr = [
                {
                    _index : 0,
                    _selected : true,
                    _yes_txt_res : "A_1_png",
                    _no_txt_res : "A_0_png"
                },
                {
                    _index : 1,
                    _selected : false,
                    _yes_txt_res : "B_1_png",
                    _no_txt_res : "B_0_png"
                }
            ];
            this._cur_index2TabBar = 0;
        }

        /**
         * 初始化页签
         */
        private initView2Tabbar() : void{
            this.initData2Tabbar();
            this.bar_target.itemRenderer = common_TabBarCell;
            this.bar_target.dataProvider = new eui.ArrayCollection( this._data2TabBar_arr );
            this.channelChange();
        }

        /**
         * 频道切换(显隐)
         */
        private channelChange() : void{
            let $cell : eui.Group = null;
            for( let $i : number = 0 , $j : number = this._all_channel.length ; $i < $j ; $i ++ ){
                $cell = this._all_channel[$i];
                $cell.visible = $i == this._cur_index2TabBar;
            }
        }

        /**
         * 销毁
         */
        public destory() : void{
            this.handlerListener2Tabber(false);
        }
    }
}

六 : 效果
Egret之Tabbar通用页签

Egret之Tabbar通用页签

2.0版优化 - 对eui.ArrayCollection 数据进行更新也可以实行页签的状态切换

module app{
    import common_TabBarCell = common.common_TabBarCell;
    import common_TabBarCell_Data = common.common_TabBarCell_Data;
    /**
     * 此UI面板测试通用的Tabbar页签Cell
     * @author Husz
     */
    export class TabBarDemoView extends eui.Component implements eui.UIComponent{
        private bar_target : eui.TabBar = null;
        private con_0 : eui.Group = null;
        private con_1 : eui.Group = null;

        private _data2TabBar_arr : Array = null;
        private _cur_index2TabBar : number = 0;
        private _all_channel : Array = null;

        private _arrayCollection : eui.ArrayCollection = null;

        public constructor(){
            super();
            this.skinName = "resource/eui_skins/TabBarDemoSkin.exml";
        }

        protected childrenCreated():void{
            super.childrenCreated();
            this._all_channel = [
                this.con_0,
                this.con_1
            ];
            this.handlerListener2Tabber( true );
            this.initView2Tabbar();
        }

        private handlerListener2Tabber( $isAdd : boolean ) : void{
            if( $isAdd ){
                if( !this.bar_target.hasEventListener(eui.ItemTapEvent.ITEM_TAP) )
                    this.bar_target.addEventListener( eui.ItemTapEvent.ITEM_TAP , this.onTabbarItemClick , this  );
            }else{
                if( this.bar_target.hasEventListener(eui.ItemTapEvent.ITEM_TAP) )
                    this.bar_target.removeEventListener( eui.ItemTapEvent.ITEM_TAP , this.onTabbarItemClick , this  );
            }
        }

        /**
         * 处理页签切换
         * @param {eui.ItemTapEvent} $e
         */
        private onTabbarItemClick( $e : eui.ItemTapEvent ) : void{
            if( this._cur_index2TabBar != $e.itemIndex ){
                this._cur_index2TabBar = $e.itemIndex;
                let $dataCell : common_TabBarCell_Data = null;
                let $count : number = 0;
                for( let $i : number = 0 , $j :number = this._data2TabBar_arr.length ; $i < $j ; $i ++ ){
                    $dataCell = this._data2TabBar_arr[$i];
                    if( $dataCell._index == this._cur_index2TabBar ){
                        $dataCell._selected = true;
                        this._arrayCollection.replaceItemAt( $dataCell , $i );
                        $count ++;
                    }else{
                        if( $dataCell._selected != false ){
                            $dataCell._selected = false;
                            this._arrayCollection.replaceItemAt( $dataCell , $i );
                            $count ++;
                        }
                    }
                    if($count >= 2){
                        break;
                    }
                }
                //切换频道
                this.channelChange();
                // this.bar_target.dataProvider = new eui.ArrayCollection( this._data2TabBar_arr );
            }
        }

        /**
         * 初始化页签数据
         */
        private initData2Tabbar() : void{
            this._data2TabBar_arr = [
                {
                    _index : 0,
                    _selected : true,
                    _yes_txt_res : "A_1_png",
                    _no_txt_res : "A_0_png"
                },
                {
                    _index : 1,
                    _selected : false,
                    _yes_txt_res : "B_1_png",
                    _no_txt_res : "B_0_png"
                }
            ];
            this._arrayCollection = new eui.ArrayCollection( this._data2TabBar_arr );
            this._cur_index2TabBar = 0;
        }

        /**
         * 初始化页签
         */
        private initView2Tabbar() : void{
            this.initData2Tabbar();
            this.bar_target.itemRenderer = common_TabBarCell;
            this.bar_target.dataProvider = this._arrayCollection;
            this.channelChange();
        }

        /**
         * 频道切换(显隐)
         */
        private channelChange() : void{
            let $cell : eui.Group = null;
            for( let $i : number = 0 , $j : number = this._all_channel.length ; $i < $j ; $i ++ ){
                $cell = this._all_channel[$i];
                $cell.visible = $i == this._cur_index2TabBar;
            }
        }

        /**
         * 销毁
         */
        public destory() : void{
            this.handlerListener2Tabber(false);
        }
    }
}

Egret之Tabbar通用页签

相关内容

热门资讯

菲律宾,没有以色列的命,得了以... 菲律宾,马尼拉。美国国务卿鲁比奥来了。菲律宾期盼已久的“救星”来了。菲律宾认为的“阳光”来了。从机场...
服务器盛会东莞启幕,松山湖软硬... 7月23日至24日,“智算筑基 融链共赢——2026年服务器产业供需对接活动”在东莞举行。作为系列配...
面向6G通信、高端安检、精准医... 太赫兹作为“电磁波段最后一块未开垦之地”,近年在超快光电子、低尺度半导体技术的推动下,逐步从实验室走...
获得全球最高奖项的中国数学家,... 一个喜欢玩梗的二次元大佬,竟然站上了数学界最高荣誉的领奖台。他就是今年的菲尔兹奖得主之一,邓煜。另一...
单向空间杭州旗舰店8年后谢幕,... 去年12月,单向空间杭州乐堤港店员工回应闭店谣言,称“没有收到任何闭店通知”。不到一年,停止营业的消...
带娃家长刷不到下铺,算法不该把... 据新华社的报道,一位宝妈带4岁娃要从昭通去杭州南,在12306上购票时反复被分到上铺,一天内取消三次...
安徽一高标准农田电机出水异常,... 近日,安徽省淮北市濉溪县百善镇丁楼村村民向《都市现场》爆料称:村里2023年建成的高标准农田配套电机...
河北一演唱会音响“翻车”,人声... 近日,来自北京的庞女士向都市现场记者反映,她7月11日在河北唐山观看“青春超燃巨星演唱会”时遭遇了极...
尺素金声|中国经济“失速论”站... 一季度GDP同比增长5.0%、二季度增长4.3%、上半年增长4.7%,2026年中国经济半年报发布后...
NVIDIA最深的护城河要凉!... 7月24日消息,AMD在Advancing AI预简报会上表示,CUDA已不再是外界想象中的那条护城...