GUI:ListView_Create 详细介绍
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-14 15:02:04 |
👁️ 阅读: 3
`GUI:ListView_Create` 是996引擎中用于创建列表容器(ListView)的核心函数。它用于创建一个可以垂直或水平滚动、并能动态加载子项的UI容器。
**函数签名:**
```lua
local listView = GUI:ListView_Create(parent, ID, x, y, width, height, direction)
```
**参数说明:**
- `parent`:父节点对象(必填)
- `ID`:控件的唯一标识字符串(必填)
- `x`:横坐标位置(必填)
- `y`:纵坐标位置(必填)
- `width`:列表宽度(必填)
- `height`:列表高度(必填)
- `direction`:滚动方向(必填)
- `1`:垂直滚动
- `0`:水平滚动
**使用示例:**
```lua
-- 创建垂直滚动的列表
local ListView = GUI:ListView_Create(parentPanel, "ListView", 143, 33, 504, 290, 1)
-- 设置背景不透明度
GUI:Layout_setBackGroundColorOpacity(ListView, 255)
-- 启用裁剪(超出部分不显示)
GUI:Layout_setClippingEnabled(ListView, true)
-- 设置重力对齐方式(5为中心对齐)
GUI:ListView_setGravity(ListView, 5)
-- 设置锚点
GUI:setAnchorPoint(ListView, 0, 1)
-- 启用触摸
GUI:setTouchEnabled(ListView, true)
```
**常用操作函数:**
```lua
-- 添加子项到末尾
GUI:ListView_pushBackCustomItem(listView, childItem)
-- 在指定位置插入子项
GUI:ListView_insertCustomItem(listView, childItem, 1)
-- 删除所有子项
GUI:ListView_removeAllItems(listView)
-- 删除指定索引的子项
GUI:ListView_removeItemByIndex(listView, index)
-- 滑动到顶部
GUI:ListView_scrollToTop(listView, 0.5, true)
-- 滑动到底部
GUI:ListView_scrollToBottom(listView, 0.5, true)
-- 刷新布局
GUI:ListView_doLayout(listView)
```
**典型应用场景:**
1. 聊天消息列表
2. 背包物品格子
3. 排行榜列表
4. 任务列表
5. 邮件列表
**注意事项:**
- 创建ListView后通常需要设置`Layout_setClippingEnabled`为true,确保内容不会溢出
- 可以通过`ListView_setGravity`设置子项的对齐方式
- 列表容器支持触摸滚动,可以设置`setTouchEnabled`为true
- 子项通常使用Layout作为模板,通过克隆或创建的方式动态添加
← 返回问答库