function item_show(parent, bagData, itemsPerRow, totalItems, containerDirection, padding, ListView_x, ListView_y,
itemWidth, itemHeight)
if GUI:Win_IsNull(parent) then
SL:print("错误:父节点为空")
return
end
if
要返回点击的具体bagData数值,需要修改点击事件处理逻辑。根据浮生梦老师前端课程内容,可以这样修改:
```lua
function item_show(parent, bagData, itemsPerRow, totalItems, containerDirection, padding, ListView_x, ListView_y,
itemWidth, itemHeight)
if GUI:Win_IsNull(parent) then
SL:Print("错误:父节点为空")
return
end
if bagData == nil then
SL:Print("错误:背包数据为空")
return
end
totalItems = totalItems or 20
totalItems = #bagData > totalItems and #bagData or totalItems
itemsPerRow = itemsPerRow or 2
containerDirection = containerDirection or 1
padding = padding or 1
itemWidth = itemWidth or 60
itemHeight = itemHeight or 60
local tick_imagePath = "res/public/1900000678_4.png" -- 勾选图片路径
local item_imgbg = "res/custom/LongMaiJueXing/60x60.png"
local startX = 20
ListView_x = ListView_x or 0
ListView_y = ListView_y or 0
local size = GUI:getContentSize(parent)
-- 创建列表容器(垂直滚动)
local listView = GUI:ListView_Create(parent, "ListView_Container", ListView_x, ListView_y,
size.width, size.height, containerDirection)
-- 存储所有item_layout的引用
local itemLayouts = {}
local function createItem_show(layout, col, x, y)
-- 创建item背景框
local itemBg = GUI:Image_Create(layout, "item_bg" .. col, x, y, item_imgbg)
return itemBg
end
for row = 1, math.ceil(totalItems / itemsPerRow) do
local layout = GUI:Layout_Create(listView, "item_layout" .. row, 0, 0, 60, 60)
-- 方向判断处理
for col = 1, itemsPerRow do
local index = (row - 1) * itemsPerRow + col
if index > totalItems then break end
local x, y = startX, 0
-- 根据容器方向计算位置
if containerDirection == 1 then -- 垂直布局
x = startX + (col - 1) * (itemWidth + padding)
y = 0
elseif containerDirection == 2 then -- 水平布局
x = 0
y = startX + (col - 1) * (itemHeight + padding)
end
local itemBg = createItem_show(layout, col, x, y)
GUI:setPosition(itemBg, x, y)
end
local children = GUI:getChildren(layout)
local index = 1
for k, v in ipairs(children) do
if bagData[index] == nil then
break
end
local itemData = {
index = bagData[index].Index,
look = true,
bgVisible = false,
count = bagData[index].OverLap,
color = 255
}
local item = GUI:ItemShow_Create(v, "ItemShow_" .. index, 15, 15, itemData)
if item then
-- 创建可点击的layout层
local item_layout = GUI:Layout_Create(item, "item_clink" .. index, -15, -15, 60, 60)
GUI:Layout_setBackGroundImage(item_layout, tick_imagePath)
GUI:setTouchEnabled(item_layout, true)
GUI:setSwallowTouches(item_layout, false)
-- 存储引用
itemLayouts[index] = item_layout
-- 修改点击事件,返回具体的bagData数值
GUI:addOnClickEvent(item_layout, function()
local clickedData = bagData[index]
if clickedData then
SL:Print("单击触发---" .. index)
SL:Print("物品ID: " .. (clickedData.Index or "未知"))
SL:Print("物品数量: " .. (clickedData.OverLap or 0))
-- 返回完整的bagData数值
return clickedData
else
SL:Print("错误:索引" .. index .. "对应的bagData不存在")
end
end)
end
index = index + 1
end
end
-- 返回listView和itemLayouts表
return listView, itemLayouts
end
```
主要修改:
1. 在点击事件中获取 `clickedData = bagData[index]`
2. 打印出具体的数值信息(物品ID和数量)
3. 通过 `return clickedData` 返回完整的bagData数据
这样当点击某个物品时,会返回该物品在bagData中的完整数据对象,包含Index、OverLap等所有属性。