
local menuData = {
{id = 1, name = "武器", subMenus = {"戒指1","项链2","手镯3"}},
{id = 2, name = "防具", subMenus = {"戒指2","项2链","手2镯"}},
{id = 3, name = "饰品", subMenus = {"戒指3","项3链","手镯3"}},
}
-- 记录当前展开的一级菜单索引
local currentExpandedIndex = nil
function createwin()
local win = GUI:Win_Create("Win_1", 0, 0, 1136, 640)
node = GUI:Node_Create(win, "node", 0, 0)
local mainListView = GUI:ListView_Create(win, "MainListView", 100, 100, 200, 400, 1)
GUI:ListView_setDirection(mainListView, 1)
GUI:ListView_setGravity(mainListView, 5) -- 设置垂直排列
GUI:setTouchEnabled(mainListView, true)
creatbutton(mainListView)
end
function creatbutton(mainListView)
for k, v in ipairs(menuData) do
local mainBtn = GUI:Button_Create(mainListView, "btn_"..v.id, 0, 0, "res/public/1900000612.png")
GUI:Button_setTitleText(mainBtn, v.name)
GUI:setContentSize(mainBtn, 180, 40)
GUI:addOnClickEvent(mainBtn, function()
-- 如果点击的是已展开的菜单,则关闭
if currentExpandedIndex == k then
removeSubMenus(mainListView, k)
currentExpandedIndex = nil
else
-- 先移除其他已展开的二级菜单
if currentExpandedIndex then
removeSubMenus(mainListView, currentExpandedIndex)
end
-- 展开当前菜单
createSubMenus(mainListView, v.subMenus, k)
currentExpandedIndex = k
SL:print("数量",GUI:ListView_getItemCount(mainListView))
for k ,v in pairs(GUI:ListView_getItems(mainListView)) do
SL:print(k ,v,GUI:getName(v))
end
GUI:ListView_doLayout(mainListView)
end
end)
end
end
-- 创建二级菜单
function createSubMenus(mainListView, subMenus, parentIndex)
for i, subName in ipairs(subMenus) do
local oldBtn = GUI:getChildByName(node, "sub_"..parentIndex.."_"..i)
if oldBtn and not GUI:Win_IsNull(oldBtn) then
GUI:removeChildByName(node, "sub_"..parentIndex.."_"..i)
end
if node and not GUI:Win_IsNull(node) then
local subBtn2 = GUI:Button_Create(node, "sub_"..parentIndex.."_"..i, 0, 0, "res/public/1900000652.png")
GUI:Button_setTitleText(subBtn2, " "..subName) -- 添加缩进
GUI:setContentSize(subBtn2, 160, 35)
-- GUI:Timeline_Window1(subBtn2, nil)
GUI:setOpacity(subBtn2, 0)
-- GUI:setOpacity(subBtn2, 255) -- 设置半透明
GUI:Timeline_FadeIn(subBtn2, 0.5, nil)
-- 计算插入位置(一级菜单下方)
local insertPos = parentIndex + i
if subBtn2 then
GUI:ListView_insertCustomItem(mainListView, subBtn2, parentIndex)
GUI:addOnClickEvent(subBtn2, function()
SL:print("选择了:", subName)
end)
GUI:ListView_setDirection(mainListView, 1)
GUI:ListView_setGravity(mainListView, 5) -- 设置垂直排列
GUI:setTouchEnabled(mainListView, true)
end
end
end
GUI:ListView_doLayout(mainListView)
end
-- 移除二级菜单
function removeSubMenus(mainListView, parentIndex)
local children = GUI:getChildren(mainListView)
for i = #children, 1, -1 do
local child = children[i]
if string.find(GUI:getName(child), "sub_") then
SL:print("移除~~",GUI:getName(child))
GUI:setOpacity(child, 0)
-- GUI:removeFromParent(child)
GUI:ListView_removeChild(mainListView, child)
end
end
GUI:ListView_doLayout(mainListView)
end
function main()
createwin()
end