[0]List的itemRender方式item全积压在左上角
自己制作预制体 preb并通过拖拽方式赋值给 NpcLists组件。运行时,预设的data 有4条,结果都挤在List的左上角,查看返现他们的每个item的y只是从0到3,每个加1。不为何会这样。
class Item extends Laya.Box{
constructor(){
super()
this.itembox = null
}
initImg(item_pref, data_obj){
if (this.numChildren==0){
this.itembox = Laya.Pool.getItemByCreateFun("preNpcItem", item_pref.create, item_pref)
//console.log("ItemNpcList",this.itembox, item_pref, data_obj)
this.addChild(this.itembox)
let npcName = this.itembox.getChildByName("NpcName")
let Level = this.itembox.getChildByName("Level")
npcName.text = data_obj.name
Level.text = data_obj.level
}
}
}
export default class NpcList extends Laya.Script {
constructor() {
super();
/** @prop {name:Preb_item, tips:"item组件的prefab", type:Prefab, default:null}*/
this.Preb_item = null
//示例数据
this.example_data=[{name:"张三",level:5},{name:"李四",level:6},{name:"张无",level:5},{name:"李六",level:6}]
}
onAwake(){
this.owner.height = Laya.stage.height;
}
onUpdate() {
//console.log(this.intType)
}
onEnable() {
//初始化
this.owner.itemRender = Item
this.owner.repeatX = 1
this.owner.selectEnable = true
this.owner.renderHandler = new Laya.Handler(this, this.onUpdateItem, null, false) //渲染
this.owner.selectHandler = new Laya.Handler(this, this.onSelect, null, false)
this.owner.array = this.example_data
}
onDisable() {
}
loadData(){
//**加载数据
console.log("NpcList_Reload")
}
//////////////////////
onUpdateItem(cell, index) {
cell.initImg(this.Preb_item, this.example_data[index])
}
onSelect(index){
console.log("NPCLIST-select:",index)
}
}
class Item extends Laya.Box{
constructor(){
super()
this.itembox = null
}
initImg(item_pref, data_obj){
if (this.numChildren==0){
this.itembox = Laya.Pool.getItemByCreateFun("preNpcItem", item_pref.create, item_pref)
//console.log("ItemNpcList",this.itembox, item_pref, data_obj)
this.addChild(this.itembox)
let npcName = this.itembox.getChildByName("NpcName")
let Level = this.itembox.getChildByName("Level")
npcName.text = data_obj.name
Level.text = data_obj.level
}
}
}
export default class NpcList extends Laya.Script {
constructor() {
super();
/** @prop {name:Preb_item, tips:"item组件的prefab", type:Prefab, default:null}*/
this.Preb_item = null
//示例数据
this.example_data=[{name:"张三",level:5},{name:"李四",level:6},{name:"张无",level:5},{name:"李六",level:6}]
}
onAwake(){
this.owner.height = Laya.stage.height;
}
onUpdate() {
//console.log(this.intType)
}
onEnable() {
//初始化
this.owner.itemRender = Item
this.owner.repeatX = 1
this.owner.selectEnable = true
this.owner.renderHandler = new Laya.Handler(this, this.onUpdateItem, null, false) //渲染
this.owner.selectHandler = new Laya.Handler(this, this.onSelect, null, false)
this.owner.array = this.example_data
}
onDisable() {
}
loadData(){
//**加载数据
console.log("NpcList_Reload")
}
//////////////////////
onUpdateItem(cell, index) {
cell.initImg(this.Preb_item, this.example_data[index])
}
onSelect(index){
console.log("NPCLIST-select:",index)
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
2 个回复
layabox
赞同来自:
cmxStar
赞同来自: