cocos 2 d-x-lua自動複列Listの追加

2217 ワード

--[[
 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