在Fyne中,特定列表项的按钮操作

huangapple go评论66阅读模式
英文:

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()是解决这个问题的方法,但我不确定它是否适用于这种情况。

编辑: 这里有一个更具图形化的示例(代码与上面的代码略有不同,但原理相同):
在Fyne中,特定列表项的按钮操作

在这种情况下,我想执行与“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):
在Fyne中,特定列表项的按钮操作

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])
            }
        },

    )
}

huangapple
  • 本文由 发表于 2022年4月23日 00:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/71971679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定