Mobile IndexBar 索引栏
为按字母或分类分组的长列表提供侧边索引,支持点按或拖动跳转并固定当前分组标题。
何时使用
- 通讯录、城市选择和按首字母分组的长列表适合使用
IndexBar。 - 分组数量少或需要搜索时,优先使用普通 List 加 SearchBar。
- 不要把 IndexBar 用于分页导航或平级内容切换,分别使用 Tabs 或 TabBar。
- 每个索引应对应一个可展示的分组,空分组应在业务层过滤。
在线体验
真实 gallery 展示城市分组、右侧索引和可点击 Cell,可拖动索引快速定位。
基本用法
indexes 的顺序决定分组顺序;Body 回调收到当前分组的下标。
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 保持身份。
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 负责行交互;两者的事件可以在同一帧处理。
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 可放置辅助操作。
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 可给索引栏分配剩余高度或固定高度;索引本身不替代外层页面布局。
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
func IndexBar(c *ui.Context, indexes []string) *IndexBarWIndexBar creates an index bar for groups titled indexes.
返回 *IndexBarW,可链式调用的方法见 IndexBarW 方法。
mobile.FloatingBubble
func FloatingBubble(c *ui.Context, ic *icon.Icon, axis Axis, magnetic bool) *FloatingBubbleWFloatingBubble 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
func SafeArea(c *ui.Context, edge Edge) *ui.BoxSafeArea 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.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body builds the groups; group(i) builds the rows of group i. | Body(group func(i int)) *IndexBarW |
Layout | Layout 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.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name of the bubble (it shows an icon only). | A11yLabel(s string) *FloatingBubbleW |
Clicked | Clicked 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.
| 字段 | 说明 | 类型 |
|---|---|---|
Name | string | |
Box | Box is the view box (min x, min y, width, height) Parts are drawn in, like an SVG viewBox. | [4]float32 |
Width | Width is the stroke width of stroked Parts in Box units. | float32 |
Parts | Parts are painted in order. | []Part |
Dir | Dir 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 的方法:
| 方法 | 说明 | 签名 |
|---|---|---|
Directional | Directional marks ic as directional, so it mirrors horizontally when drawn for a right-to-left layout (see DrawOpts.RTL), and returns ic. | Directional() *Icon |
IsDirectional | IsDirectional reports whether ic mirrors in right-to-left layouts. | IsDirectional() bool |
IsImage | IsImage reports whether ic is a bitmap icon made by FromImage. | IsImage() bool |