Skip to content

Mobile IndexBar 索引栏 ​

为按字母或分类分组的长列表提供侧边索引,支持点按或拖动跳转并固定当前分组标题。

何时使用 ​

  • 通讯录、城市选择和按首字母分组的长列表适合使用 IndexBar。
  • 分组数量少或需要搜索时,优先使用普通 List 加 SearchBar。
  • 不要把 IndexBar 用于分页导航或平级内容切换,分别使用 Tabs 或 TabBar。
  • 每个索引应对应一个可展示的分组,空分组应在业务层过滤。

在线体验 ​

真实 gallery 展示城市分组、右侧索引和可点击 Cell,可拖动索引快速定位。

Mobile · IndexBar / FloatingBubble / SafeArea · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

indexes 的顺序决定分组顺序;Body 回调收到当前分组的下标。

go
import (
    "github.com/tangoui/tango/ui"
    "github.com/tangoui/tango/ui/layout"
    "github.com/tangoui/tango/ui/mobile"
)

var groups = []string{"A", "B", "C"}

func page(c *ui.Context) {
    mobile.IndexBar(c, groups).Layout(layout.Grow(1)).Body(func(i int) {
        ui.Text(c, groups[i])
        mobile.Cell(c, "分组 "+groups[i]).Clickable()
    })
}

常见用法 ​

城市分组 ​

用 map 保存分组内容,Body 中用索引下标取值;动态行用 ui.Key 保持身份。

go
var cities = map[string][]string{
    "A": {"安庆", "鞍山", "安阳"},
    "B": {"北京", "保定"},
}

func cityList(c *ui.Context) {
    mobile.IndexBar(c, groups).Layout(layout.Grow(1)).Body(func(i int) {
        for _, name := range cities[groups[i]] {
            name := name
            ui.Key(c, name, func() { mobile.Cell(c, name).Clickable() })
        }
    })
}

点击行 ​

IndexBar 负责索引跳转,Cell 负责行交互;两者的事件可以在同一帧处理。

go
func clickableGroups(c *ui.Context) {
    mobile.IndexBar(c, []string{"A", "B"}).Body(func(i int) {
        cell := mobile.Cell(c, "条目").Clickable()
        if cell.Clicked() {
            ui.Text(c, "选择了第 "+fmt.Sprint(i)+" 组")
        }
    })
}

配合安全区和浮动操作 ​

底部安全区避免最后一组被系统手势条遮挡;FloatingBubble 可放置辅助操作。

go
import "github.com/tangoui/tango/ui/icon"

func safeIndex(c *ui.Context) {
    mobile.IndexBar(c, []string{"A", "B"}).Layout(layout.Grow(1)).Body(func(i int) {
        mobile.Cell(c, "分组 "+groups[i])
    })
    mobile.SafeArea(c, mobile.Bottom)
    mobile.FloatingBubble(c, icon.Message, mobile.AxisBoth, true).A11yLabel("客服")
}

自定义布局 ​

Layout 可给索引栏分配剩余高度或固定高度;索引本身不替代外层页面布局。

go
func fixedIndex(c *ui.Context) {
    mobile.IndexBar(c, []string{"A", "B", "C"}).Layout(layout.H(420)).Body(func(i int) {
        mobile.Cell(c, fmt.Sprintf("第 %d 组", i+1))
    })
}

注意事项 ​

索引切片和分组数据应保持稳定顺序,回调下标只在当前构建中有效。长列表内容放在稳定的页面状态中,动态行使用 ui.Key。底部安全区由宿主或 Provider 提供时不要重复计算;为浮动按钮设置 A11yLabel。RTL 会把索引移到另一侧,业务不要写死右侧坐标。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Mobile · IndexBar / FloatingBubble / SafeArea

gallery 演示 MobileIndexbar 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

mobile.IndexBar ​

go
func IndexBar(c *ui.Context, indexes []string) *IndexBarW

IndexBar creates an index bar for groups titled indexes.

返回 *IndexBarW,可链式调用的方法见 IndexBarW 方法。

mobile.FloatingBubble ​

go
func FloatingBubble(c *ui.Context, ic *icon.Icon, axis Axis, magnetic bool) *FloatingBubbleW

FloatingBubble adds a floating bubble showing ic. axis limits dragging (AxisBoth by default); magnetic snaps it to the left or right edge. It starts in the bottom end corner (bottom right; bottom left in RTL).

ConfigProvider defaults ("mobile.FloatingBubble"): "size" (number, at least the 44pt touch minimum; default 48).

返回 *FloatingBubbleW,可链式调用的方法见 FloatingBubbleW 方法。

mobile.SafeArea ​

go
func SafeArea(c *ui.Context, edge Edge) *ui.Box

SafeArea adds a blank of the safe-area inset of edge (Top: status bar / notch, Bottom: home indicator), filled with the box color.

返回通用容器 *ui.Box,可用的布局与样式方法见 Box 的 API。

IndexBarW 方法 ​

mobile.IndexBarW:IndexBarW is a grouped list with an alphabet index at the end (on the right; on the left in RTL): tap or drag along the index to jump to a group. The current group's title sticks to the top.

方法说明签名
BodyBody builds the groups; group(i) builds the rows of group i.Body(group func(i int)) *IndexBarW
LayoutLayout applies layout options to the container (give it a height).Layout(opts ...layout.Option) *IndexBarW

FloatingBubbleW 方法 ​

mobile.FloatingBubbleW:FloatingBubbleW is a draggable round button that floats over the page and sticks to the nearest side when released.

方法说明签名
A11yLabelA11yLabel sets the accessible name of the bubble (it shows an icon only).A11yLabel(s string) *FloatingBubbleW
ClickedClicked reports a tap on the bubble.Clicked() bool

Icon 参数类型 ​

icon.Icon:Icon is a vector (or bitmap, see FromImage) icon. Declare custom icons with a composite literal, New or FromImage:

var Branch = &icon.Icon{Name: "app:branch", Box: [4]float32{0, 0, 1024, 1024},
	Parts: []icon.Part{{D: "M..."}}}

The exported fields must not change once the icon is first drawn.

字段说明类型
Namestring
BoxBox is the view box (min x, min y, width, height) Parts are drawn in, like an SVG viewBox.[4]float32
WidthWidth is the stroke width of stroked Parts in Box units.float32
PartsParts are painted in order.[]Part
DirDir marks a directional icon (an arrow, a chevron, text lines that start at the left): it mirrors horizontally in right-to-left layouts.bool

Icon 的方法:

方法说明签名
DirectionalDirectional marks ic as directional, so it mirrors horizontally when drawn for a right-to-left layout (see DrawOpts.RTL), and returns ic.Directional() *Icon
IsDirectionalIsDirectional reports whether ic mirrors in right-to-left layouts.IsDirectional() bool
IsImageIsImage reports whether ic is a bitmap icon made by FromImage.IsImage() bool

相关链接 ​

以 MIT 许可发布