function lib:createitemlist(winid, data, interval, hide_bg, has_eff, default_color, dataitem, has_opc_parent, effectid,
item_bg_path, not_scale, omit_val, count_color)
local check = winid and type(data) == "table" and true or false
local
你需要在函数外部添加一个标记来控制布局只执行一次。根据浮生梦老师前端课程内容,可以使用一个静态变量来记录是否已经执行过布局。
```lua
-- 在函数外部定义标记
local hasLayoutExecuted = false
function lib:createitemlist(winid, data, interval, hide_bg, has_eff, default_color, dataitem, has_opc_parent, effectid,
item_bg_path, not_scale, omit_val, count_color)
local check = winid and type(data) == "table" and true or false
local item_bg = {}
if not check then
return item_bg
end
local has_dataitem = dataitem
GUI:removeAllChildren(winid)
local item_data = {}
local item_count = data.count or 0
local iswin32 = SL:GetMetaValue("WINPLAYMODE")
if item_count > 0 then
for i = 1, item_count do
if data["type" .. i] == "货币" or data["type" .. i] == "双货币" or data["type" .. i] == "物品"
or data["type" .. i] == "称号" or data["type" .. i] == "称号2" then
local cfg = {}
cfg.type = data["type" .. i]
cfg.name = data["name" .. i]
cfg.value = data["value" .. i]
cfg.takename = data["takename" .. i]
cfg.bg_eff = data["bg_eff" .. i]
table.insert(item_data, cfg)
end
end
end
for i, v in ipairs(item_data) do
--特殊处理
if data.count and data.count == 1 and dataitem then
dataitem = dataitem
else
dataitem = SL:GetMetaValue("ITEM_DATA", lib:getitemidx(v.name))
end
--local path = iswin32 and ConstCfg.pc_item_bg_path or ConstCfg.mobile_item_bg_path
local path = ConstCfg.pc_item_bg_path
if item_bg_path then
path = item_bg_path
end
item_bg = GUI:Image_Create(winid, "item_bg" .. i, 0.00, 0.00, path)
GUI:Win_SetParam(item_bg, i)
GUI:setTouchEnabled(item_bg, true)
GUI:setTag(item_bg, i)
if v.bg_eff then
--背景光圈配置
local bg_eff_str = v.bg_eff[1]
if pc_client then
bg_eff_str = v.bg_eff[2]
end
--切割配置
local bg_eff_cfg = lib:split_str(bg_eff_str, "#")
if bg_eff_cfg and next(bg_eff_cfg) then
local item_gq_eff = GUI:Effect_Create(item_bg, "item_gq_eff" .. i, bg_eff_cfg[2] or 0, bg_eff_cfg[3] or 0,
0, bg_eff_cfg[1] or 15132, 0, 0, 0, 1)
GUI:setScale(item_gq_eff, bg_eff_cfg[4] or 1)
end
elseif has_eff then
--光圈特效
local wp_Eff = GUI:Effect_Create(item_bg, "item_eff" .. i, 25.00, 26.00, 0,
effectid or 15132, 0, 0, 0, 1)
if not iswin32 and not not_scale then
GUI:setScale(wp_Eff, 1.1)
GUI:setPosition(wp_Eff, 30, 30)
end
end
local item_bg_size = GUI:getContentSize(item_bg)
--物品框
local item_show = GUI:ItemShow_Create(item_bg, "item_show", item_bg_size.width / 2, item_bg_size.height / 2,
{
index = dataitem.Index,
look = true,
bgVisible = false,
count = 0,
--count = v.value > 1 and v.value or 0,
itemData = dataitem,
})
GUI:setAnchorPoint(item_show, 0.5, 0.5)
if v.value and v.value >= 1 and not has_dataitem then
local curbagcount = SL:GetMetaValue("ITEM_COUNT", v.name)
if v.type == "称号" then
curbagcount = lib:check_title(v.name) and v.value or 0
end
if v.type == "称号2" then
SL:Print("称号2 =", v.takename)
curbagcount = lib:check_title(v.takename) and v.value or 0
end
if v.type == "双货币" then
curbagcount = tonumber(SL:GetMetaValue("MONEY_ASSOCIATED", lib:getitemidx(v.name)))
end
local val = (omit_val and v.value <= 1) and "" or "x" .. lib:getFormatNumStr(v.value)
local Text_xiaohao = GUI:Text_Create(item_bg, "Text_xiaohao", item_bg_size.width - 4, 10.00, 14,
curbagcount >= v.value and "#00ff00" or "#FF0000", val)
if default_color then
GUI:Text_setTextColor(Text_xiaohao, "#ffffff")
end
GUI:setTouchEnabled(Text_xiaohao, false)
GUI:setTag(Text_xiaohao, -1)
GUI:setAnchorPoint(Text_xiaohao, 1, 0.50)
if count_color then
if type(count_color) == "number" then
count_color = SL:GetHexColorByStyleId(count_color)
end
GUI:Text_setTextColor(Text_xiaohao, count_color)
end
end
if hide_bg and not has_opc_parent then
GUI:setOpacity(item_bg, 0)
end
if has_opc_parent then
GUI:setCascadeOpacityEnabled(item_bg, true)
end
end
-- 修改这里:只执行一次布局
if not hasLayoutExecuted and GUI:getDescription(winid) == "Layout" then
local layout = {}
layout.dir = 2
layout.addDir = 2
layout.gap = { x = interval and interval or 20 }
layout.sortfunc = function(lists)
table.sort(lists, function(a, b)
return GUI:getTag(a) > GUI:getTag(b)
end)
end
if not hide_bg then
layout.interval = 1
end
GUI:UserUILayout(winid, layout)
hasLayoutExecuted = true -- 标记已执行
end
return item_bg
end
```
**修改说明:**
1. 在函数外部添加 `local hasLayoutExecuted = false` 作为标记变量
2. 将条件判断改为 `if not hasLayoutExecuted and GUI:getDescription(winid) == "Layout" then`
3. 在执行布局后设置 `hasLayoutExecuted = true`
4. 这样布局代码只会在第一次调用函数时执行
**注意事项:**
- 这个标记是模块级别的,只对当前模块有效
- 如果需要每次打开界面都重新布局,可以在界面关闭时重置标记
- `GUI:UserUILayout()` 函数用于自动排列子控件,参数说明:
- `dir`: 排列方向(2可能是水平方向)
- `addDir`: 添加方向
- `gap`: 间距设置
- `sortfunc`: 排序函数
- `interval`: 间隔设置
这样修改后,布局代码就只会在第一次打开界面时执行一次了。