var ScrollViewTestingLayer=cc.Layer.extend({ ctor:function () { this._super(); // Create the list view var listView = new ccui.ListView(); // set list view ex direction listView.setDirection(ccui.ScrollView.DIR_VERTICAL); listView.setTouchEnabled(true); listView.setBounceEnabled(true); listView.setBackGroundImage(resStartGame.background); listView.setBackGroundImageScale9Enabled(true); listView.setContentSize(cc.size(240, 130)); listView.x = gameConfig.CenterPoint.x; listView.y = gameConfig.CenterPoint.y; listView.addEventListener(this.selectedItemEvent, this); this.addChild(listView); var default_item = new ccui.Layout(); default_item.setTouchEnabled(true); default_item.setContentSize(cc.size(300,20)); default_item.width = listView.width; //default_item.addChild(default_button); // set model listView.setItemModel(default_item); for (var i = 0; i < 20; ++i) { listView.pushBackDefaultItem();//注意这一行,相当重要!!!内部占坑用的,我就奇怪设计api的时候直接让传个参数进去内部调用不也行么?官方二不兮兮的这么封装我也是醉了 } for(var i=0;i<20;i++){ var lblMenu=new cc.LabelBMFont(i.toString(),resFont.font1); var lblLayer=new ccui.Layout(); lblMenu.setContentSize(cc.size(100,20)); lblMenu.width=80; lblMenu.x=50; lblMenu.y=20*-1*i; lblLayer.addChild(lblMenu); listView.insertCustomItem(lblLayer); } // set all items layout gravity listView.setGravity(ccui.ListView.GRAVITY_CENTER_VERTICAL); }, selectedItemEvent: function (sender, type) { cc.log("selectedItemEvent"); switch (type) { case ccui.ListView.EVENT_SELECTED_ITEM: var listViewEx = sender; cc.log("select child index = " + listViewEx.getCurSelectedIndex()); break; default: break; } } });
官方的例子太TMD复杂了,这是个简版的。
尤其注意注释中的那句话。