cocos2d-x -lua 自动多列List的添加

--[[
主要对listview做自动行列的添加
hgc
]]
HListView = class("HListView", function(view)
    return view
end)

--[[
@view已经创建的listview
@col多少列
@itemSize每一项的大小
]]
function HListView:create(view,col)
    HListView._col = col

    local listView = HListView.new(view) 
    if listView then
        listView:initUI()
    end
    return listView
end

function HListView:initUI()
    self._itemCount = 0
    self._currentIndex = 1
    self._itemList = {}
end

function HListView:addItem(item)
    item:show()
    local itemSize = item:getContentSize()
    local size = self:getContentSize()

    if self._itemCount % self._col == 0 then
        self._currentIndex = 1
        self._layoutNode = ccui.Layout:create()
        g_insertItemInListView(self,self._layoutNode)
    end

    local space = (size.width - self._col*itemSize.width)/(self._col-1)
    local x = (self._itemCount % self._col) * (itemSize.width + space)
    local y = itemSize.height/2
    g_setNodePosition(item,cc.p(0,0.5),cc.p(x,y))
    self._layoutNode:addChild(item)

    self._itemCount = self._itemCount + 1
    self._itemList[self._itemCount] = item

    self._layoutNode:setContentSize(cc.size(self._currentIndex*itemSize.width, itemSize.height))
    self._currentIndex = self._currentIndex + 1
end

--[[
返回所有项
]]
function HListView:getList()
    return self._itemList
end

--遍历所有项执行一个函数
function HListView:traversal(func)
    for i,v in ipairs(self._itemList) do
        if func then func(v) end 
    end
end

--[[
清空所有
]]
function HListView:clear()
    self:removeAllItems()
    self._itemList = {}
    self._itemCount = 0
    self._currentIndex = 1
end

使用

function UIHhChoseFlag:initFlagList()
    self.m_ListView_FlagAll = HListView:create(self.m_ListView_FlagAll,5)
    for k, v in ipairs({}) do
        local item = self.m_Panel_FlagItem:clone()
        self.m_ListView_FlagAll:addItem(item)
        self:updateGoodsItem(item,v)
    end
end

--道具每一项
function UIHhChoseFlag:updateGoodsItem(item,data)
    item:show()
    local goodsId = data.goods.goodsId
    local imgIcon = g_seekWidgetByName(item,"Image_Goods_Icon")
    item:loadTexture(g_getGoodsBoxPath(goodsId),1)
    g_loadTextureGoodsIcon(imgIcon,nil,goodsId)
end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容