--自定义属性
function ItemTips.GetCustomShow(parent, data, isPetItem)
if not data or next(data) == nil then
return nil
end
if not parent then
return
end
local str = ""
local list = GUI:ListView_Create(parent, "Cust
在浮生梦老师的前端课程中,`ItemTips.GetCustomShow` 函数用于创建自定义属性显示。根据你提供的代码,属性已经通过 `group = d.i or 0` 进行了分组。
如果你想在**两个不同分组之间**添加一条横线,可以修改循环逻辑。核心思路是:在遍历 `data.abil` 时,检查当前分组 `group` 是否与上一个分组不同,如果不同且不是第一个分组,则在中间插入一个分割线。
以下是修改后的代码片段,展示了如何添加横线:
```lua
--自定义属性
function ItemTips.GetCustomShow(parent, data, isPetItem)
if not data or next(data) == nil then
return nil
end
if not parent then
return
end
local str = ""
local list = GUI:ListView_Create(parent, "Custom_list", 0, 0, 0, 0, 1)
GUI:setTouchEnabled(list, false)
GUI:ListView_setItemsMargin(list, _setVspace or 0)
local listSize = {width = 0, height = 0}
local lastGroup = nil -- 用于记录上一个分组
for p, d in pairs(data.abil or {}) do
local isShowAttr = true
if (not d.t or d.t == "") and not isPetItem then
isShowAttr = false
end
-- 检查是否需要添加分组横线
local currentGroup = d.i or 0
if lastGroup ~= nil and currentGroup ~= lastGroup then
-- 在两个不同分组间添加分割线
local linePanel = ItemTips.CreateIntervalPanel(list, _DefaultSpace, true)
if linePanel then
local lineSize = GUI:getContentSize(linePanel)
listSize.height = listSize.height + lineSize.height + (_setVspace or 0)
listSize.width = math.max(listSize.width, lineSize.width)
end
end
lastGroup = currentGroup -- 更新记录的分组
if isShowAttr then
local title = ItemTips.GetCustomTitleShow(p, list, d.t, d.c)
if title then
local titleSize = GUI:getContentSize(title)
listSize.height = listSize.height + titleSize.height + (_setVspace or 0)
listSize.width = math.max(listSize.width, titleSize.width)
end
end
-- ... [后续处理attList和属性显示的代码保持不变] ...
-- 注意:为了清晰起见,这里省略了后续很长的属性生成代码
-- 你只需要将上面这段“检查分组并添加横线”的逻辑,插入到你的原代码中 for p, d in pairs 循环的合适位置即可。
end
-- ... [函数剩余部分保持不变] ...
end
```
**关键点说明:**
1. **`lastGroup` 变量**:用于追踪上一次循环处理的分组ID。
2. **添加横线的时机**:在每次循环开始处理一个新条目 `d` 时,判断其分组 `currentGroup` 是否与 `lastGroup` 不同,并且 `lastGroup` 不为 `nil` (即不是第一个分组)。
3. **使用现有函数**:`ItemTips.CreateIntervalPanel(parent, height, line)` 是课程中已有的创建分割线的函数。参数 `line` 为 `true` 时会创建横线图片。
4. **更新尺寸**:添加横线后,需要更新 `listSize` 的宽高,以确保容器尺寸正确。
**使用方法:**
将上述“检查分组并添加横线”的代码块,放入你原函数中 `for p, d in pairs(data.abil or {}) do` 循环的开头部分,紧接着变量定义之后即可。这样,每当遇到一个新的分组,就会自动在前一个分组内容后添加一条横线。