function npc:bag_item_show(p1, p2)
local ListView_2 = self.ui.ListView_2
GUI:setVisible(ListView_2, true)
GUI:setVisible(self.ui.item_colse, true)
local item_bg = self.ui.item
GUI:ListView_removeAllItems(ListView_2)
-- 获取背包石头
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-31 12:04:42 |
👁️ 阅读: 1
```lua
-- 封装背包物品显示和点击处理函数
function createBagItemDisplay(parentUI, itemList, pageSize, callbackFunc)
-- 参数说明:
-- parentUI: 父UI对象
-- itemList: 背包物品列表
-- pageSize: 每页显示数量(默认9个)
-- callbackFunc: 点击回调函数,参数为(itemData, index, page, posInPage)
-- 创建分页显示容器
local function createPagedContainer(itemList, pageSize)
pageSize = pageSize or 9
local pageCount = math.max(math.ceil(#itemList / pageSize), 3)
local containers = {}
for i = 1, pageCount do
local itemNode = GUI:Clone(parentUI.item) -- 克隆模板
GUI:setVisible(itemNode, true)
GUI:setName(itemNode, "item_node_" .. i)
table.insert(containers, itemNode)
end
return containers, pageCount
end
-- 创建物品显示项
local function createItemDisplay(itemNode, itemData, posIndex, callback)
local operMode = SL:GetMetaValue("CURRENT_OPERMODE")
local itemBgName = "item_bg_" .. posIndex
local itemShowBg = GUI:getChildByName(itemNode, itemBgName)
if not GUI:Win_IsNotNull(itemShowBg) then
return nil
end
-- 创建物品显示
local itemShow = GUI:ItemShow_Create(
itemShowBg,
"item_show_" .. posIndex,
operMode == 1 and 13 or 0,
operMode == 1 and 13 or 1,
{
index = itemData.Index,
look = true,
bgVisible = false,
count = itemData.OverLap or 1,
color = itemData.Color or 0,
}
)
-- 创建点击区域
local clickArea = GUI:Layout_Create(
itemShowBg,
"item_click_" .. posIndex,
0, 0, 60, 60,
false
)
GUI:setTouchEnabled(clickArea, true)
GUI:setSwallowTouches(clickArea, false)
-- 添加点击事件
GUI:addOnClickEvent(clickArea, function()
-- 点击动画效果(自定义函数,需要自行实现)
-- fangzhi_liandian(clickArea, 0.3)
-- 执行回调
if type(callback) == "function" then
callback(itemData, posIndex)
end
end)
return itemShow, clickArea
end
-- 填充物品到分页
local function fillItemsToPages(containers, itemList, pageSize, callback)
pageSize = pageSize or 9
for idx, itemData in ipairs(itemList) do
local page = math.ceil(idx / pageSize)
local posInPage = (idx - 1) % pageSize + 1
if containers[page] then
createItemDisplay(containers[page], itemData, posInPage, function(item, pos)
if callback then
callback(item, idx, page, posInPage)
end
end)
end
end
end
-- 主流程
local containers, pageCount = createPagedContainer(itemList, pageSize)
-- 将容器添加到ListView
local listView = parentUI.ListView_2
if GUI:Win_IsNotNull(listView) then
GUI:ListView_removeAllItems(listView)
for _, container in ipairs(containers) do
GUI:ListView_pushBackCustomItem(listView, container)
end
end
-- 填充物品
fillItemsToPages(containers, itemList, pageSize, callbackFunc)
-- 显示相关UI
GUI:setVisible(listView, true)
if parentUI.item_colse then
GUI:setVisible(parentUI.item_colse, true)
end
return {
containers = containers,
pageCount = pageCount,
updateItems = function(newItemList)
-- 更新物品列表
itemList = newItemList or itemList
fillItemsToPages(containers, itemList, pageSize, callbackFunc)
end,
clearAll = function()
-- 清空所有显示
if GUI:Win_IsNotNull(listView) then
GUI:ListView_removeAllItems(listView)
end
end
}
end
-- 使用示例:
-- 1. 在UI初始化时调用
-- local bagDisplay = createBagItemDisplay(self.ui, npc.bag_item, 9, function(itemData, index, page, pos)
-- -- 处理点击事件
-- local tab = {
-- MakeIndex = itemData.MakeIndex,
-- id = p1,
-- Index = p2,
-- }
-- GUIUtil:Lua_SubmitForm("星阵", "Xz_qiangxian", tab.MakeIndex .. "#" .. tab.id .. "#" .. tab.Index)
--
-- SL:ScheduleOnce(function()
-- npc:upde_interface(tab.id)
-- self:colse_bagitem()
-- self:upde_interface(p1)
-- end, 0.3)
-- end)
-- 2. 更新物品列表
-- bagDisplay.updateItems(newItemList)
-- 3. 清空显示
-- bagDisplay.clearAll()
```
这个封装函数提供了以下功能:
1. **分页显示**:自动根据物品数量分页,每页可自定义数量
2. **点击回调**:通过回调函数处理点击事件,与业务逻辑解耦
3. **动态更新**:可以随时更新物品列表
4. **内存管理**:提供清空功能,避免内存泄漏
注意:函数中的 `fangzhi_liandian` 是自定义动画函数,需要根据实际项目实现。
← 返回问答库