[]list item 添加事件后无法监听
刚接触laya不久,按官方文档写了list的demo,但是发现item的点击/ select一直没回调,这是我的代码,求大神解答
// 初始化ListView
function createListView(x, y, width, height, item_width, item_height) {
var list = new Laya.List();
list.itemRender = initItem(item_width, item_height);
list.repeatX = 1;
list.size(width, height);
list.x = x;
list.y = y;
list.vScrollBarSkin = "";
list.mouseHandler = new Handler(this, onSelect)
list.selectEnable = true;
list.selectHandler = new Handler(this, onSelect);
list.renderHandler = new Handler(this, updateItem);
return list;
}
function updateItem(cell, index) {
console.log(index);
// console.log(cell.dataSource);
cell.setImg(cell.dataSource[index]);
cell.on(Laya.Event.CLICK, function() {
console.log("click")
})
}
function onSelect(index) {
console.log("当前选择的索引:" + index);
}
function createAPIList(api_list_view) {
let data = [];
for (let i = 0; i < 10; i++) {
let text_view = createTextView("12345", "black", 18, innerWidth, 35, "white");
data.push(text_view)
}
return data;
}
function drawAPIList() {
let api_list_view = createListView(0, cur_y + 45, innerWidth, innerHeight - cur_y - 30, innerWidth - 30, 45);
api_list_view.dataSource = createAPIList();
Laya.stage.addChild(api_list_view);
}
// 初始化ListView
function createListView(x, y, width, height, item_width, item_height) {
var list = new Laya.List();
list.itemRender = initItem(item_width, item_height);
list.repeatX = 1;
list.size(width, height);
list.x = x;
list.y = y;
list.vScrollBarSkin = "";
list.mouseHandler = new Handler(this, onSelect)
list.selectEnable = true;
list.selectHandler = new Handler(this, onSelect);
list.renderHandler = new Handler(this, updateItem);
return list;
}
function updateItem(cell, index) {
console.log(index);
// console.log(cell.dataSource);
cell.setImg(cell.dataSource[index]);
cell.on(Laya.Event.CLICK, function() {
console.log("click")
})
}
function onSelect(index) {
console.log("当前选择的索引:" + index);
}
function createAPIList(api_list_view) {
let data = [];
for (let i = 0; i < 10; i++) {
let text_view = createTextView("12345", "black", 18, innerWidth, 35, "white");
data.push(text_view)
}
return data;
}
function drawAPIList() {
let api_list_view = createListView(0, cur_y + 45, innerWidth, innerHeight - cur_y - 30, innerWidth - 30, 45);
api_list_view.dataSource = createAPIList();
Laya.stage.addChild(api_list_view);
}
没有找到相关结果
已邀请:
要回复问题请先登录
2 个回复
Aar0n
赞同来自:
Aar0n
赞同来自:
(function()
{
// 项渲染器
var Box = Laya.Box;
var Image = Laya.Image;
var WID = 373,
HEI = 85;
function Item()
{
Item.__super.call(this);
this.size(WID, HEI);
this.img = new Image();
this.addChild(this.img);
this.setImg = function(src)
{
this.img.skin = src;
}
}
Laya.class(Item, "Item", Box);
// 主要逻辑代码
var Stage = Laya.Stage;
var List = Laya.List;
var Handler = Laya.Handler;
var WebGL = Laya.WebGL;
(function()
{
// 不支持WebGL时自动切换至Canvas
Laya.init(800, 600, WebGL);
Laya.stage.alignV = Stage.ALIGN_MIDDLE;
Laya.stage.alignH = Stage.ALIGN_CENTER;
Laya.stage.scaleMode = Stage.SCALE_SHOWALL;
Laya.stage.bgColor = "#232628";
setup();
})();
function setup()
{
var list = new List();
list.itemRender = Item;
list.repeatX = 1;
list.repeatY = 4;
list.x = (Laya.stage.width - WID) / 2;
list.y = (Laya.stage.height - HEI * list.repeatY) / 2;
// 使用但隐藏滚动条
list.vScrollBarSkin = "";
list.selectEnable = true;
list.selectHandler = new Handler(this, onSelect);
list.renderHandler = new Handler(this, updateItem);
Laya.stage.addChild(list);
// 设置数据项为对应图片的路径
var data = [];
for (var i = 0; i < 10; ++i)
{
data.push("../../res/ui/listskins/1.jpg");
data.push("../../res/ui/listskins/2.jpg");
data.push("../../res/ui/listskins/3.jpg");
data.push("../../res/ui/listskins/4.jpg");
data.push("../../res/ui/listskins/5.jpg");
}
list.array = data;
}
function updateItem(cell, index)
{
cell.setImg(cell.dataSource);
}
function onSelect(index)
{
console.log("当前选择的索引:" + index);
}
})();