英文:
Button action for a specific list item in Fyne
问题
我在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”按钮所属的仓库相关的操作。
英文:
I have a list in the GUI which has a simple string slice as its data source. And for every list item I create a button that should do something for that specific list item.
Here is some example code:
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])
		},
    )
}
How can I wire up the button to the list item? I need a way to know which exact button was pressed or a way that the button knows on which list item it sits (or which list item is his parent).
Is there a way to do this?
Maybe widget.NewListWithData() is something that would address this issue but I'm unsure if this will help in this case.
Edit: Here is a more graphical example to show this (the code for this is slightly different but the principles are the same as with the code above):

In this case I want to execute the pull for the one repo which the "pull" button belongs to.
答案1
得分: 1
你可以通过分配 Button.OnTapped 来设置按钮的功能。
英文:
You can set the button function by assigning Button.OnTapped
答案2
得分: 1
Credit goes to @andy.xyz as he pointed me in the right direction.
I just want to provide some example code if people are searching for a solution.
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[0].(*widget.Label).SetText(data[id])
            // new part
            item.(*fyne.Container).Objects[1].(*widget.Button).OnTapped = func() {
                fmt.Println("I am button " + data[id])
            }
        },
    )
}
英文:
Credit goes to @andy.xyz as he pointed me in the right direction.
I just want to provide some example code if people are searching for a solution.
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[0].(*widget.Label).SetText(data[id])
            // new part
            item.(*fyne.Container).Objects[1].(*widget.Button).OnTapped = func() {
                fmt.Println("I am button " + data[id])
            }
        },
    )
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论