ListItem的设置

ListItem的设置是ScrollView和自定义每行Item的结合

1、创建一个ScrollView

2、创建一个节点Templates

3、在Templates创建一个精灵Item,该节点存放的东西为每行类似的东西,比如每行都有用户名和俱乐部ID等等,在精灵Item上可以添加精灵Sprite和文本Label。

4、创建一个脚本Item并且精灵Item控件关联这个脚本,

const i18n = require('i18n');

cc.Class({

    extends: cc.Component,

    properties: {

        label: {

            default: null,

            type: cc.Label

        },

        itemID: 0

    },

    onLoad: function () {

        this.node.on('touchend', function () {

            console.log("Item " + this.itemID + ' clicked');

        }, this);

    },

    updateItem: function(tmplId, itemId) {

        this.itemID = itemId;

        this.label.textKey = i18n.t("cases/02_ui/05_scrollView/Item.js.1") + tmplId + ' Item#' + this.itemID;

    },

});

5、脚本和编辑器设置一些属性(总共多少项、)

 

properties:{

        itemTemplate: { // item template to instantiate other items

            default: null,

            type: cc.Node

        },

 

        spawnCount: 0, // how many items we actually spawn

        totalCount: 0, // how many items we need for the whole list

        spacing: 0, // space between each item

        bufferZone: 0, // when item is away from bufferZone, we relocate it

        lblScrollEvent: cc.Label,

},

onLoad:function(){

this.content = this.scrollView.content;

this.items = []; // array to store spawned items
         this.lastContentPosY = 0;

this.initialize();

},

initialize: function () {

        this.content.height = this.totalCount * (this.itemTemplate.height + this.spacing) + this.spacing; // get total content height

         for (let i = 0; i < this.totalCount; ++i) { // spawn items, we only need to do this once

                   let item = cc.instantiate(this.itemTemplate);

                   this.content.addChild(item);

                   item.setPosition(0, -item.height * (0.5 + i) - this.spacing * (i + 1));

                   item.getComponent('Item').updateItem(i, i);

            this.items.push(item);

         }

},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值