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通用页签

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...