scroll - 使用 GXUI 在 Go 中使窗口滚动

标签 scroll go scrollbar gxui

我正在尝试使用 GXUI 将滚动条添加到 Go 中的应用程序窗口。

假设我有这个代码:

package main

import (
    "fmt"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/samples/flags"
    "github.com/google/gxui/themes/dark"
)

func appMain(driver gxui.Driver) {
    theme := dark.CreateTheme(driver)

    window := theme.CreateWindow(800, 600, "Grid")
    window.SetScale(flags.DefaultScaleFactor)
    window.OnClose(driver.Terminate)

    row := theme.CreateLinearLayout()
    row.SetDirection(gxui.LeftToRight)
    for c := 0; c < 4; c++ {
        col := theme.CreateLinearLayout()
        col.SetDirection(gxui.TopToBottom)
        for r := 0; r < 100; r++ {
            cell := theme.CreateLabel()
            cell.SetText(fmt.Sprintf("%d", r*4+c))
            col.AddChild(cell)
        }
        row.AddChild(col)
    }

    window.AddChild(row)
}

func main() {
    gl.StartDriver(appMain)
}

当我运行它时,我得到这个窗口:

enter image description here

如何让窗口有滚动条以便我可以查看所有行?

最佳答案

我无法使用帮助ScrollLayout,但我可以根据 github 中的示例提出此变体。

package main

import (
    "fmt"
    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/math"
    "github.com/google/gxui/samples/flags"
    "github.com/google/gxui/themes/dark"
)

type customAdapter struct {
    gxui.AdapterBase
}

func (a *customAdapter) Count() int {
    return 1000
}

func (a *customAdapter) ItemAt(index int) gxui.AdapterItem {
    return index
}

func (a *customAdapter) ItemIndex(item gxui.AdapterItem) int {
    return item.(int)
}

func (a *customAdapter) Size(theme gxui.Theme) math.Size {
    return math.Size{W: 200, H: 25}
}

func (a *customAdapter) Create(theme gxui.Theme, index int) gxui.Control {

    layout1 := theme.CreateLinearLayout()
    layout1.SetDirection(gxui.LeftToRight)
    for c := 0; c < 4; c++ {
        col := theme.CreateLinearLayout()
        col.SetDirection(gxui.TopToBottom)
        cell := theme.CreateLabel()
        cell.SetText(fmt.Sprintf("%d", index*4+c))
        col.AddChild(cell)
        layout1.AddChild(col)
    }
    return layout1
}

func appMain(driver gxui.Driver) {
    theme := dark.CreateTheme(driver)
    window := theme.CreateWindow(600, 400, "Grid")
    window.BorderPen()
    window.SetScale(flags.DefaultScaleFactor)
    window.OnClose(driver.Terminate)
    adapter := &customAdapter{}
    list := theme.CreateList()
    list.SetAdapter(adapter)
    list.SetOrientation(gxui.Vertical)
    window.AddChild(list)
}

func main() {
    gl.StartDriver(appMain)
}

每一行都放在列表中,它们的数量和大小在重写的方法中指定。优点是列表中已经有了滚动条。

关于scroll - 使用 GXUI 在 Go 中使窗口滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280709/

相关文章:

go - 将多个字符修剪到最终斜杠的右侧,包括斜杠

javascript - niceScroll 光标高度

jquery - 使用 Perfect-scrollbar 插件在同一页面中添加多个滚动条

jquery - 如何防止最大高度过渡将页面滚动到顶部?

encoding - 如何在 Go 中将位保存到文件中

go - go 如何知道从哪里获取包

html - 滚动条向下移动页面

javascript - 如何创建可无限滚动的 div(y 轴)?

javascript - html/js中没有点击功能的滚动页面 Action

javascript - 在 scrollabe 之上创建不可滚动的 div