local benyuanzhili = {}
function benyuanzhili.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
benyuanzhili.parent = lib:openwin(param1, nil, true, param4)
你的代码现在的问题是子按钮的ListView直接创建在`ui.magic_list_view`上,并且没有管理好位置,导致点击时其他按钮不会下移。你需要让子按钮的ListView作为主按钮的子节点,并且动态调整其他主按钮的位置。
以下是修改后的关键部分:
```lua
-- 在 upmain 函数中创建主按钮时,为每个主按钮创建一个容器Widget来承载子按钮的ListView
for nameid = 1, 6 do
local magic_id = nameid
if not benyuanzhili.magic_btns[magic_id] then
-- 创建主按钮
local btn = GUI:Button_Create(ui.magic_list_view, "magic_btn_"..magic_id, 0, 0,
"res/yidalu/benyuanzhili/" .. magic_id .. "_1.png")
benyuanzhili.magic_btns[magic_id] = btn
GUI:addOnClickEvent(btn, function()
benyuanzhili.click_magic_btn(magic_id)
end)
-- 为每个主按钮创建一个容器Widget,用来放置子按钮ListView
-- 这个容器初始不可见,点击时显示并调整位置
local container = GUI:Widget_Create(ui.magic_list_view, "container_"..magic_id, 0, 0, 300, 100)
GUI:setVisible(container, false)
benyuanzhili.childContainers[magic_id] = container
-- 在容器中创建ListView
local listView = GUI:ListView_Create(container, "ListView_"..magic_id, 0, 0, 300, 100, 1)
benyuanzhili.childListViewList[magic_id] = listView
end
end
-- 修改 click_magic_btn 函数
function benyuanzhili.click_magic_btn(magic_id)
local ui = benyuanzhili.parent._ui
-- 如果点击的是当前已展开的按钮,则收缩
if benyuanzhili.click_magic_id == magic_id then
-- 收缩当前按钮的子按钮
local curContainer = benyuanzhili.childContainers[magic_id]
if curContainer then
GUI:setVisible(curContainer, false)
end
local curBtn = benyuanzhili.magic_btns[magic_id]
if curBtn then
GUI:Button_loadTextureNormal(curBtn, "res/yidalu/benyuanzhili/" .. magic_id .. "_1.png")
GUI:setTouchEnabled(curBtn, true)
end
-- 重置所有按钮位置
benyuanzhili.resetButtonPositions()
benyuanzhili.click_magic_id = nil
return
end
-- 隐藏旧按钮的子按钮
if benyuanzhili.click_magic_id then
local oldID = benyuanzhili.click_magic_id
local oldBtn = benyuanzhili.magic_btns[oldID]
if oldBtn then
GUI:Button_loadTextureNormal(oldBtn, "res/yidalu/benyuanzhili/" .. oldID .. "_1.png")
GUI:setTouchEnabled(oldBtn, true)
end
local oldContainer = benyuanzhili.childContainers[oldID]
if oldContainer then
GUI:setVisible(oldContainer, false)
GUI:removeAllChildren(benyuanzhili.childListViewList[oldID])
end
end
-- 设置当前按钮
benyuanzhili.click_magic_id = magic_id
local curBtn = benyuanzhili.magic_btns[magic_id]
GUI:Button_loadTextureNormal(curBtn, "res/yidalu/benyuanzhili/" .. magic_id .. "_2.png")
GUI:setTouchEnabled(curBtn, false)
-- 显示子按钮容器
local container = benyuanzhili.childContainers[magic_id]
if container then
GUI:setVisible(container, true)
-- 设置容器的位置在主按钮下方
local btnPos = GUI:getPosition(curBtn)
GUI:setPosition(container, btnPos.x, btnPos.y - 50) -- 调整到主按钮下方
-- 创建子按钮
local lv = benyuanzhili.childListViewList[magic_id]
GUI:removeAllChildren(lv)
for i = 1, 6 do
local child = GUI:Button_Create(lv, "child_"..magic_id.."_"..i, 0, 0,
"res/yidalu/benyuanzhili/10_1.png")
GUI:ListView_pushBackCustomItem(lv, child)
end
-- 调整其他按钮的位置
benyuanzhili.adjustButtonPositions(magic_id)
end
end
-- 新增函数:调整按钮位置
function benyuanzhili.adjustButtonPositions(expandedMagicId)
local ui = benyuanzhili.parent._ui
local buttonHeight = 50 -- 假设每个按钮高度为50
local subButtonHeight = 100 -- 子按钮容器高度
for id = 1, 6 do
local btn = benyuanzhili.magic_btns[id]
if btn and GUI:Win_IsNotNull(btn) then
local yPos = (id - 1) * buttonHeight
if id > expandedMagicId then
yPos = yPos + subButtonHeight -- 在展开按钮下方的按钮需要下移
end
GUI:setPosition(btn, 0, yPos)
end
end
end
-- 新增函数:重置按钮位置
function benyuanzhili.resetButtonPositions()
local ui = benyuanzhili.parent._ui
local buttonHeight = 50
for id = 1, 6 do
local btn = benyuanzhili.magic_btns[id]
if btn and GUI:Win_IsNotNull(btn) then
local yPos = (id - 1) * buttonHeight
GUI:setPosition(btn, 0, yPos)
end
end
end
-- 在 benyuanzhili 表中添加 childContainers
benyuanzhili.childContainers = {}
```
**主要改动说明:**
1. **为每个主按钮创建容器**:使用`GUI:Widget_Create`为每个主按钮创建一个容器Widget,用来放置子按钮的ListView。
2. **位置调整函数**:
- `adjustButtonPositions`:当某个按钮展开时,调整在它下方的所有按钮的位置
- `resetButtonPositions`:当按钮收缩时,重置所有按钮到原始位置
3. **点击逻辑优化**:
- 点击已展开的按钮会收缩子按钮
- 收缩时重置所有按钮位置
- 展开时调整下方按钮位置
4. **坐标计算**:
- 假设每个主按钮高度为50
- 子按钮容器高度为100
- 在展开按钮下方的所有按钮需要下移100像素
这样修改后,点击主按钮展开子按钮时,下方的按钮会下移;再次点击收缩时,所有按钮会回到原始位置。