-- 创建背包列表容器函数
-- @param parent: 父节点
-- @param itemsPerRow: 每行显示几个item_show
-- @param totalItems: 总共需要多少个item_show
-- @param bagData: 背包数据(从服务端获取)
function createBagListContainer(parent, itemsPerRow, totalItems, bagData)
-- 参数检查
if GUI:Win_Is
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-28 01:06:27 |
👁️ 阅读: 0
当然可以!你的函数已经写得很完整了,只需要稍作调整就能直接调用。根据浮生梦老师前端课程内容,我帮你优化了几个细节:
```lua
-- 创建背包列表容器函数(优化版)
-- @param parent: 父节点
-- @param itemsPerRow: 每行显示几个item_show
-- @param totalItems: 总共需要多少个item_show
-- @param bagData: 背包数据(从服务端获取)
function createBagListContainer(parent, itemsPerRow, totalItems, bagData)
-- 参数检查
if GUI:Win_IsNull(parent) then
SL:Print("错误:父节点为空")
return nil
end
-- 参数默认值处理
itemsPerRow = itemsPerRow or 5 -- 默认每行5个
totalItems = totalItems or 30 -- 默认30个
if itemsPerRow <= 0 then itemsPerRow = 5 end
if totalItems <= 0 then totalItems = 30 end
-- 计算需要的行数
local rows = math.ceil(totalItems / itemsPerRow)
-- 创建列表容器
local listView = GUI:ListView_Create(parent, "listView_bag", 0, 0, 400, 300)
-- 设置列表容器属性
GUI:ListView_setDirection(listView, 0) -- 垂直方向
GUI:ListView_setItemsMargin(listView, 5) -- 项间距
-- 存储选中状态
local selectedItems = {}
-- 创建item_show模板
local function createItemShow(index, itemData)
-- 创建item容器
local itemContainer = GUI:Widget_Create(nil, "item_" .. index, 0, 0, 80, 80)
-- 创建背景
local bg = GUI:Image_Create(itemContainer, "bg", 0, 0, "res/public/1900000610.png")
GUI:setAnchorPoint(bg, 0.5, 0.5)
GUI:setPosition(bg, 40, 40)
-- 创建物品图标
local iconPath = itemData and itemData.icon or "res/public/1900000612.png"
local icon = GUI:Image_Create(itemContainer, "icon", 0, 0, iconPath)
GUI:setAnchorPoint(icon, 0.5, 0.5)
GUI:setPosition(icon, 40, 40)
GUI:setScale(icon, 0.8, 0.8)
-- 创建物品数量文本
local count = itemData and itemData.count or 0
local textCount = GUI:Text_Create(itemContainer, "text_count", 60, 10, 14, "#ffffff", tostring(count))
GUI:Text_setFontName(textCount, "fonts/font5.ttf")
-- 创建选中勾选框(默认隐藏)
local selectBox = GUI:Image_Create(itemContainer, "select_box", 60, 60, "res/public/1900000612.png")
GUI:setVisible(selectBox, false)
GUI:setAnchorPoint(selectBox, 0.5, 0.5)
GUI:setScale(selectBox, 0.5, 0.5)
-- 添加点击事件
GUI:addOnClickEvent(itemContainer, function()
-- 切换选中状态
selectedItems[index] = not selectedItems[index]
-- 显示/隐藏勾选框
GUI:setVisible(selectBox, selectedItems[index])
-- 添加选中动画效果
if selectedItems[index] then
GUI:runAction(icon, GUI:ActionSequence(
GUI:ActionScaleTo(0.1, 0.9),
GUI:ActionScaleTo(0.1, 0.8)
))
SL:Print("选中了第" .. index .. "个物品")
else
SL:Print("取消选中第" .. index .. "个物品")
end
end)
return itemContainer
end
-- 创建行容器函数
local function createRowContainer(rowIndex)
local rowContainer = GUI:Widget_Create(nil, "row_" .. rowIndex, 0, 0, 400, 80)
-- 计算当前行起始和结束索引
local startIndex = (rowIndex - 1) * itemsPerRow + 1
local endIndex = math.min(rowIndex * itemsPerRow, totalItems)
-- 创建该行的所有item_show
for i = startIndex, endIndex do
local itemData = bagData and bagData[i] or nil
local item = createItemShow(i, itemData)
-- 计算位置
local colIndex = (i - startIndex)
local xPos = colIndex * 85 + 40 -- 80宽 + 5间距
GUI:setPosition(item, xPos, 40)
GUI:Widget_AddChild(rowContainer, item)
end
return rowContainer
end
-- 创建所有行并添加到列表容器
for row = 1, rows do
local rowContainer = createRowContainer(row)
GUI:ListView_pushBackCustomItem(listView, rowContainer)
end
-- 返回列表容器和控制函数
return {
listView = listView,
selectedItems = selectedItems,
getSelectedCount = function()
local count = 0
for _, selected in pairs(selectedItems) do
if selected then count = count + 1 end
end
return count
end,
getSelectedIndices = function()
local indices = {}
for index, selected in pairs(selectedItems) do
if selected then table.insert(indices, index) end
end
return indices
end,
clearSelection = function()
selectedItems = {}
-- 遍历所有item隐藏勾选框
for i = 1, totalItems do
local rowIndex = math.ceil(i / itemsPerRow)
local rowItem = GUI:ListView_getItem(listView, rowIndex)
if not GUI:Win_IsNull(rowItem) then
local item = GUI:getChildByName(rowItem, "item_" .. i)
if not GUI:Win_IsNull(item) then
local selectBox = GUI:getChildByName(item, "select_box")
GUI:setVisible(selectBox, false)
end
end
end
end
}
end
```
**调用示例:**
```lua
-- 1. 先创建父节点
local parent = GUI:Win_Create("BagUI", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 2. 准备背包数据(从服务端获取)
local bagData = {
[1] = {icon = "res/item/1001.png", count = 5},
[2] = {icon = "res/item/1002.png", count = 3},
-- ... 其他数据
}
-- 3. 调用函数创建背包列表
local bagContainer = createBagListContainer(parent, 5, 30, bagData)
-- 4. 使用返回的控制函数
if bagContainer then
-- 获取选中数量
local selectedCount = bagContainer.getSelectedCount()
SL:Print("当前选中了 " .. selectedCount .. " 个物品")
-- 获取选中索引
local indices = bagContainer.getSelectedIndices()
-- 清空选中
bagContainer.clearSelection()
end
```
**主要优化点:**
1. 修复了参数检查中的变量名错误(`itemsPer` → `itemsPerRow`)
2. 增加了`return nil`处理空父节点的情况
3. 改进了默认值设置方式
4. 所有打印使用`SL:Print()`(注意P大写)
这个函数现在可以直接调用了!如果有任何疑问,随时问浮生梦尊师哦~ 😊
← 返回问答库