function npc.main()
-- npc.jj_data = longmaishuju
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0, false, false, true, true, true, 0)
GUI:LoadExport(parent, "UI/XingShiHuFuUI")
npc._parent = parent
npc.ui = {}
npc.ui = GUI:ui_delegate(parent)
s
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-29 20:33:36 |
👁️ 阅读: 1
根据浮生梦老师前端课程内容,我来帮你分析这个需求。你的代码思路基本正确,但需要调整克隆和插入的逻辑,并添加点击事件来管理组件切换。
主要问题:
1. 你需要在点击主按钮时动态插入对应的克隆组件
2. 点击新按钮时要移除之前插入的组件
3. 需要记录当前显示的克隆组件以便删除
修改后的代码:
```lua
function npc.main()
-- npc.jj_data = longmaishuju
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0, false, false, true, true, true, 0)
GUI:LoadExport(parent, "UI/XingShiHuFuUI")
npc._parent = parent
npc.ui = {}
npc.ui = GUI:ui_delegate(parent)
ssrSetWidgetPosition(parent, npc.ui.Image_1, 2, 0, 0, true, true)
-- 关闭按钮
GUI:addOnClickEvent(npc.ui.close, function()
npc.Close()
end)
-- 存储当前显示的克隆组件
npc.currentClone = nil
-- 获取ListView中的主按钮
local mainButtons = {}
for k, child in ipairs(GUI:ListView_getItems(npc.ui.ListView_1)) do
local name = GUI:getName(child)
SL:print("按钮名称:", name, "索引:", k)
-- 只处理Button_1到Button_4
if name == "Button_1" or name == "Button_2" or name == "Button_3" or name == "Button_4" then
mainButtons[k] = child
-- 为每个主按钮添加点击事件
GUI:addOnClickEvent(child, function()
-- 移除之前显示的克隆组件
if npc.currentClone and not GUI:Win_IsNull(npc.currentClone) then
GUI:removeFromParent(npc.currentClone)
npc.currentClone = nil
end
-- 克隆新的Panel
local newPanel = GUI:Clone(npc.ui.Panel_1)
GUI:setVisible(newPanel, true)
-- 插入到当前按钮后面
local itemIndex = GUI:ListView_getItemIndex(npc.ui.ListView_1, child)
GUI:ListView_insertCustomItem(npc.ui.ListView_1, newPanel, itemIndex + 1)
-- 记录当前克隆组件
npc.currentClone = newPanel
SL:print("点击了按钮:", name, "插入位置:", itemIndex + 1)
end)
end
end
-- 入场动画
Action_slow_appear(npc._parent, 200, 3, 1)
end
```
关键点说明:
1. **获取按钮索引**:使用`GUI:ListView_getItemIndex(npc.ui.ListView_1, child)`获取每个按钮在ListView中的位置索引
2. **动态插入**:在按钮点击事件中,使用`GUI:ListView_insertCustomItem(npc.ui.ListView_1, newPanel, itemIndex + 1)`将克隆组件插入到对应按钮后面
3. **组件管理**:使用`npc.currentClone`变量记录当前显示的克隆组件,点击新按钮时先删除之前的
4. **组件删除**:使用`GUI:removeFromParent(npc.currentClone)`从父节点移除组件
5. **空值判断**:使用`GUI:Win_IsNull()`检查组件是否有效
这样实现后,点击Button_1会在它后面插入克隆的Panel_1,点击Button_2时会先删除Button_1插入的Panel,然后在Button_2后面插入新的Panel,以此类推。
注意:`Action_slow_appear`函数需要确保在知识库中存在,如果不存在需要自定义实现。
← 返回问答库