我在 GUI 中有一个列表,它有一个简单的字符串切片作为它的数据源。对于每个列表项,我都创建了一个按钮,该按钮应该为该特定列表项执行某些操作。
这是一些示例代码:
var data = []string{"folder1", "folder2"}
...
func someListCreationMethod(data []string) *widget.List {
return widget.NewList(
func() int {
return len(data)
},
func() fyne.CanvasObject {
return container.NewPadded(
widget.NewLabel("Will be replaced"),
widget.NewButton("Do Something", nil),
)
},
func(id widget.ListItemID, item fyne.CanvasObject) {
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(data[id])
},
)
}
如何将按钮连接到列表项?我需要一种方法来知道按下了哪个按钮或按钮知道它位于哪个列表项(或哪个列表项是他的父项)的方法。
有没有办法做到这一点?
也许widget.NewListWithData()可以解决这个问题,但我不确定这对这种情况是否有帮助。
编辑:这是一个更形象的例子来展示这一点(代码略有不同,但原理与上面的代码相同):
在这种情况下,我想对“pull”按钮所属的一个 repo 执行 pull。
呼如林
天涯尽头无女友
相关分类