笔记六 :EgretH5通用MVC框架的入门操作:ItemList滚动列表的使用,以及内部Item配置与监听的方法(UI部分)

笔记六 EgretH5通用MVC框架的入门操作:ItemList滚动列表的使用,以及内部Item配置与监听的方法(UI部分)

        前言:ItemList滚动列表是几乎所有界面UI包括网页,手机APP,游戏UI都常用到的一个组件,用于显示一些重复性的列表,比如淘宝的商品列表,微信的消息列表等等,这次笔记记录如何用Egret通用MVC框架组建ItemList滚动列表,基于笔记一中搭建的scene

源代码:笔记六Egret滚动列表ItemList

演示效果如下:

 

1.创建\src\example\module\demo\list\ItemListDemoController.ts         ( ItemList界面控制器)

/**
 * Created by egret on 15-1-7.
 */
class ItemListDemoController extends BaseController {
    private itemListDemoView:ItemListDemoView;

    public constructor() {
        super();

        this.itemListDemoView = new ItemListDemoView(this, LayerManager.UI_Popup);
        App.ViewManager.register(ViewConst.ItemListDemo, this.itemListDemoView);
    }
}

2.添加ViewConst:\src\example\consts\ViewConst.ts    ( 用于ViewManager的管理)

添加上:ItemListDemo关键字

/**
 * Created by Administrator on 2014/11/23.
 */
enum ViewConst{
    Loading = 10000,
    Login,
    Home,
    Friend,
    Shop,
    Warehouse,
    Factory,
    Task,
    Daily,
    Mail,
    Demo,
    TarBarDemo,
    ItemListDemo,

    Game = 20000,
    GameUI,
    RpgGame,
}

3.创建\src\example\module\demo\list\ItemListDemoView.ts     ( ItemList界面的View)

/**
 * Created by egret on 15-1-7.
 */
class ItemListDemoView extends BaseEuiView {
    public constructor($controller: BaseController, $parent: eui.Group) {
        super($controller, $parent);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.dispose,this);
        this.skinName = "resource/skins/demo/ItemListDemoSkin.exml";
    }

    closeBtn: eui.Button;
    arrCollection: eui.ArrayCollection;
    list:eui.List;
    
    public initUI(): void {
        //this.removeEventListener(eui.UIEvent.COMPLETE, this.initUI, this)
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseBtn, this);
        this.initItemList();
    }

    /**初始化itemList组件 */
    private initItemList():void{
        let self = this;
        /*
        let data = {
			[0]:{
				name:"角色",
				flag:false,
                info:0
			},		
			[1]:{
				name:"合击",
				flag:false,
                info:1
			},			
			[2]:{
				name:"转生",
				flag:false,
                info:2
			},			
			[3]:{
				name:"羽翼",
				flag:false,
                info:3
			},
		}
        self.arrCollection = new eui.ArrayCollection();       
        self.arrCollection.addItem(data[0])
		self.arrCollection.addItem(data[1])
		self.arrCollection.addItem(data[2])
		self.arrCollection.addItem(data[3])
        */
         let data = [{
				name:"角色",
				flag:false,
                info:0
			},{
				name:"合击",
				flag:false,
                info:1
			},{
				name:"转生",
				flag:false,
                info:2
			},{
				name:"转生",
				flag:false,
                info:3
			},{
				name:"转生",
				flag:false,
                info:4
			},{
				name:"羽翼",
				flag:false,
                info:5
         }]
        self.arrCollection = new eui.ArrayCollection(data);                
        self.list.itemRenderer = ItemDemoItemView;
        self.list.dataProvider = self.arrCollection;
    }

    private onCloseBtn(): void {
        App.ViewManager.playcloseView(1,this);
    }

    public dispose():void{
        this.closeBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseBtn, this);
    }
}

4.创建\resource\skins\demo\ItemListDemoSkin.exml   ( ItemList界面的View的skin文件)

<?xml version="1.0" encoding="utf-8"?>
<e:Skin class="ItemListDemoSkin" width="500" height="400" xmlns:e="http://ns.egret.com/eui"
        xmlns:w="http://ns.egret.com/wing">
	<e:Group width="400" height="300" anchorOffsetX="0" anchorOffsetY="0" horizontalCenter="0" verticalCenter="0">
		<e:Group width="400" height="300" verticalCenter="0" horizontalCenter="0">
			<e:Rect fillColor="0x097993" width="380" x="11" bottom="16" top="16" />
			<e:Image source="table_box_down" x="0" bottom="0" anchorOffsetX="0" width="398" />
			<e:Image source="table_box_up" x="0" top="0" anchorOffsetX="0" width="395" />
			<e:Image source="table_box_middle" x="0" top="46" bottom="93" anchorOffsetX="0" width="398" />
		</e:Group>
		<e:Rect width="398" height="300" x="2" y="0" anchorOffsetX="0" anchorOffsetY="0" fillColor="0xffffff" scaleX="1"
		        scaleY="1" visible="false" />
		<e:Scroller width="366" height="260" anchorOffsetX="0" anchorOffsetY="0" y="20" scaleX="1" scaleY="1"
		            horizontalCenter="0">
			<e:Group top="0" horizontalCenter="0">
				<e:List id="list" y="6" itemRendererSkinName="ItemDemoItemView" x="20" horizontalCenter="0" anchorOffsetX="0" width="315">
					<!--<e:ArrayCollection>
						<e:Array>
							<e:Object/>
							<e:Object/>
							<e:Object/>
							<e:Object/>
							<e:Object/>
							<e:Object/>
						</e:Array>
					</e:ArrayCollection>-->
					<e:layout>
						<e:TileLayout id="giftOpenShowListLayout" horizontalAlign="left" requestedColumnCount="1" columnWidth="100"
						               rowHeight="96" paddingTop="30"/>
					</e:layout>
				</e:List>
			</e:Group>
		</e:Scroller>
		<e:Button id="closeBtn" label="" x="328" y="1" anchorOffsetX="0" width="72" scaleX="1" scaleY="1">
			<e:skinName>
				<e:Skin states="up,down,disabled">
					<e:Image width="100%" height="100%" source="btn_wrong" source.down="btn_wrong" source.disabled="btn_wrong" />
					<e:Label id="labelDisplay" horizontalCenter="0" verticalCenter="0" />
				</e:Skin>
			</e:skinName>
		</e:Button>
	</e:Group>
</e:Skin>

 

5.创建文件:\src\example\module\demo\list\ItemDemoItemView.ts ( ItemList界面中的Item的View文件)

class ItemDemoItemView extends eui.ItemRenderer {
    constructor() {
        super();
        this.addEventListener(eui.UIEvent.COMPLETE, this.uiCompHandler, this);
        this.once(egret.Event.REMOVED_FROM_STAGE, this.dispose, this);
        this.skinName = "resource/skins/demo/ItemDemoSkin.exml";
    }

    private titleDisplay: eui.Label;
    private itembutton: eui.Button;

    private uiCompHandler(): void {
        this.removeEventListener(eui.UIEvent.COMPLETE, this.uiCompHandler, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onButtonHandler, this);
        this.initData();
    }

    private onButtonHandler(e: egret.TouchEvent): void {
        if (this.data.info != null) {  //注意0是false
            console.log("点击了这个按钮", this.data.info);
        }
    }

    public initData() {
        this.titleDisplay.text = "初始化";
    }

    public dataChanged(): void {
        this.titleDisplay.text = this.data.name;
    }

    public dispose(): void {
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onButtonHandler, this);
    }
}

6.创建\resource\skins\demo\ItemDemoSkin.exml    (item的皮肤文件)

<?xml version="1.0" encoding="utf-8"?>
<e:Skin class="ItemDemoSkin" width="325" height="96" xmlns:e="http://ns.egret.com/eui">
	<e:Image source="card_bg" anchorOffsetY="0" height="93" anchorOffsetX="0" width="319" scale9Grid="30,30,100,100"/>
	<e:Group height="34" width="197.79" x="21.5" y="32.5" anchorOffsetX="0">
		<e:Label id="titleDisplay" text="普通化肥" textColor="16777215" size="21" bold="true" horizontalCenter="-20.5" verticalCenter="3.5"/>
		<e:Image source="icon_diamond" scaleX="0.5" scaleY="0.5" width="60" y="2" x="167.79"/>
		<e:Image source="icon_02" x="-3" y="-5.5"/>
	</e:Group>
	<e:Button id="itembutton" label="B" x="228.89" y="32.5" anchorOffsetX="0" width="60" anchorOffsetY="0" height="34"/>
</e:Skin>

7.打开\src\example\module\demo\DemoView.ts (为itemlist绑定好触发打开的按钮事件)

添加上按钮组件:

 button2: eui.ToggleButton;

initUI():void 中添加上按钮的监听:

this.button2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.button2ClickHandler, this);

监听的事件函数:

    private button2ClickHandler(e: egret.TouchEvent): void {
        App.ViewManager.playopen(1, ViewConst.ItemListDemo);
    }

8.打开\src\example\test\DemoTest.ts  (在demoTest中注册ItemListDemoController)

在    private initModule():void函数中加入:

 App.ControllerManager.register(ControllerConst.ItemListDemo, new ItemListDemoController());    

DemoTest.ts 参考代码:

/**
 * Created by yangsong on 15-3-27.
 * GUI测试
 */
class DemoTest{
    public constructor(){
        var groupName:string = "preload_DemoTest";
        var subGroups:Array<string> = ["preload_core", "preload_ui"];
        App.ResourceUtils.loadGroups(groupName, subGroups, this.onResourceLoadComplete, this.onResourceLoadProgress, this);
    }

    /**
     * 资源组加载完成
     */
    private onResourceLoadComplete():void {
        this.initModule();
        App.Init();

        //音乐音效处理
        App.SoundManager.setBgOn(true);
        App.SoundManager.setEffectOn(!App.DeviceUtils.IsHtml5 || !App.DeviceUtils.IsMobile);

        //App.SceneManager.runScene(SceneConsts.UI);
        App.SceneManager.runScene(SceneConsts.Demo);
    }

    /**
     * 资源组加载进度
     */
    private onResourceLoadProgress(itemsLoaded:number, itemsTotal:number):void {
        App.ControllerManager.applyFunc(ControllerConst.Loading, LoadingConst.SetProgress, itemsLoaded, itemsTotal);
    }

    /**
     * 初始化所有模块
     */
    private initModule():void{
        App.ControllerManager.register(ControllerConst.Friend, new FriendController());
        App.ControllerManager.register(ControllerConst.Demo, new DemoController());
        App.ControllerManager.register(ControllerConst.TarbarDemo, new TarBarDemoController());      
         App.ControllerManager.register(ControllerConst.ItemListDemo, new ItemListDemoController());    
    }
}

9.运行测试(源代码接下来几个笔记后会贴出来供参考)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
src.zip中包含:DelegateUtil.ts ,Dictionary.ts ,BC.ts,Main.ts DelegateUtil.ts 函数代理,方便传递参数和函数执行空间地址,包含原始参数和附加参数。 Dictionary.ts 支持存取少量的“对象”来当作存储数据的key. 重点见介绍下面的类,养成习惯模块销毁时用一句 BC.removeEvent(this);可以有效防止出现遗漏移除的监听. 还有BC.addOnceEvent(...);方法是也是懒人必不可少的方法。 BC类在actionscript中已经用了将近8年了,版本陆陆续续改过5次,这几天根据ts的特性修改了一个版本,值得推荐给大家。 BC.ts : 支持模糊移除事件,“BC.removeEvent(this, ”后面3个参数为模糊参数,可有可无,明确的参数必须相等才会移除. * 000 删除所有关于监听者的所有事件,通常在类销毁时使用一次 BC.removeEvent(this); * 001 指定相同回调函数的所有监听 BC.removeEvent(this,null,null,func); * 010 指定事件名的所有监听 BC.removeEvent(this,null,Event.COMPLETE,null); * 011 指定事件名,指定回调函数的所有监听 BC.removeEvent(this,null,Event.COMPLETE,func); * 100 删除指定通知者 和 监听者之间的所有监听 BC.removeEvent(this,dispatch,null,null); * 101 删除通知者 和 监听者之间使用同一回调函数的所有监听 BC.removeEvent(this,dispatch,null,func); * 110 删除通知者 和 监听者之间指定事件的所有监听 BC.removeEvent(this,dispatch,Event.COMPLETE,null); * 111 明确删除指定的事件监听 BC.removeEvent(this,dispatch,Event.COMPLETE,func); 代码: class Main extends egret.DisplayObjectContainer{ //申明一个广播对象“dispatchSprite” private dispatchSprite: egret.Sprite; public constructor() { super(); //字典使用对象作为key引用存储数据.对象作为key实际上需要进行遍历索引,所以在同一个字典中尽量不要添加过多的key会影响性能. var dic: Dictionary = new Dictionary(); var arr1: string[] = ["我是数组"]; var obj2: any = { name: "我是对象" }; var str3: string = "我是字符"; //添加到字典 dic.add(arr1, arr1); dic.add(obj2, obj2); dic.add(str3, str3); //打印字典内部的数据 dic.dump(); //申明一个广播对象“dispatchSprite” this.dispatchSprite = new egret.Sprite(); //添加一个自动会移除监听事件 BC.addOnceEvent(this, this.dispatchSprite, egret.Event.ENTER_FRAME, this.onEnterFrameOnce); //创建和监听一个Timer事件 var timer: egret.Timer = new egret.Timer(50); BC.addEvent(this, timer, egret.TimerEvent.TIMER, this.onEnterTimer); timer.start(); } /** * dispatchSprite的帧事件 */ public onE

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值