Skip to main content
版本:v1.0

SCROLL_LIST

scroll_list_sample

创建一块支持滑动的列表区域,每个列表的 item 中可以设置图片和文字。

创建 UI 控件

const scrollList = hmUI.createWidget(hmUI.widget.SCROLL_LIST, Param)

类型

Param: object

属性说明是否必须类型
x控件 x 坐标number
y控件 y 坐标number
w控件显示宽度number
h控件显示高度number
item_space每个 item 之间的间距number
item_configitem 配置信息 见下方说明ItemConfig
item_config_countitem_config 数组长度number
data_array数据集合 为一个结构体数组 见下方说明DataArray
data_count数据集合长度number
item_click_funcitem 点击回调function
data_type_config每行数据要使用哪个 item 类型的配置信息 见下方说明array
data_type_config_count集合长度number
on_page更新数据时使用,设置为 1 表示更新数据后保留在当前 pagenumber

ItemConfig: object

属性说明是否必须类型
type_id当前 item 类型 Id js 自己保证不重复 如果 item_config_count 为 0 时可不填,其他时候必填number
item_heightitem 高number
item_bg_coloritem 背景色number
item_bg_radiusitem 背景圆角number
text_viewtextView 结构体数组 每一项为一个 textView 见下方说明Array<TextView>
text_view_counttext_view 数组长度number
image_viewimageView 数组 每一项为一个 imageView 见下方说明Array<ImageView>
image_view_countimage_view 数组长度number

item_config 完整配置

const item_config = [
{
type_id: 1,
item_bg_color: 0xef5350,
item_bg_radius: 10,
text_view: [
{
x: 100,
y: 0,
w: 100,
h: 20,
key: 'name',
color: 0xffffff
},
{
x: 0,
y: 30,
w: 100,
h: 100,
key: 'age',
color: 0xffffff,
text_size: 20
}
],
text_view_count: 2,
item_height: 130
},
{
type_id: 2,
item_bg_color: 0xef5350,
item_bg_radius: 10,
text_view: [{ x: 100, y: 0, w: 100, h: 20, key: 'like', color: 0x3d5afe }],
text_view_count: 1,
item_height: 100
}
]

TextView: object

涉及位置信息的全是相对坐标

属性说明是否必须类型
xx 坐标number
yy 坐标number
w控件宽度number
h控件长度number
color文字颜色number
text_size字体大小number
key数据绑定 key 详情见使用例子和 data_array 说明string
// 数组内的每一个结构体为一个 textView
const text_view = [
{ x: 100, y: 0, w: 100, h: 20, key: 'name', color: 0xffffff },
{ x: 0, y: 30, w: 100, h: 100, key: 'age', color: 0xffffff, text_size: 20 }
]

ImageView: object

涉及位置信息的全是相对坐标

属性说明是否必须类型
xx 坐标number
yy 坐标number
w控件宽度number
h控件长度number
key数据绑定 key 详情见使用例子和 data_array 说明string
// 数组内的每一个结构体为一个imageView
const image_view = [{ x: 0, y: 0, w: 20, h: 20, key: 'img_src' }]

data_array 配置

  • 每个 key 为上方添加 View 时的 key 值
  • 渲染时会从每个 View 中拿到每个 view 的 key 值去 data_array 中查询
const dataList = [
{ img_src: rootPath + 'step/step_num_1.png', name: 'name1', age: '12' },
{ img_src: rootPath + 'step/step_num_1.png', name: 'name1', age: '13' },
{ img_src: rootPath + 'step/step_num_1.png', name: 'name1', age: '13' }
]

data_type_config 配置信息

如果配置的 typeiditem_config 的第 0 项 则可省略配置

属性说明是否必须类型
start开始的索引number
end结束的索引number
type_id配置的 type_idnumber

startend 的关系为闭区间 [start, end]

{
data_type_config:[
//代表索引从0到2的数据使用type_id为2的样式
{
start: 0,
end: 2,
type_id: 2,
},
{
start: 3,
end: 10,
type_id: 1,
},
],
data_type_config_count:2
}

刷新数据

const scrollList = hmUI.createWidget(hmUI.widget.SCROLL_LIST, Param)
scrollList.setProperty(hmUI.prop.UPDATE_DATA, {
//重新设置配置信息
data_type_config: [
{
start: 0,
end: 2,
type_id: 2
}
],
//配置信息长度
data_type_config_count: 1,
//新的数据
data_array: [
{ img_src: rootPath + 'test/normalbtn_h.png', name: '姓名', age: '12', like: 'type2' },
{ img_src: rootPath + 'test/normalbtn_h.png', name: 'namex1', age: '13', like: 'type2' },
{ img_src: rootPath + 'test/normalbtn_h.png', name: 'namex2', age: '13', like: 'type2' },
{ img_src: rootPath + 'test/normalbtn_h.png', name: 'namex3', age: '12', like: 'type2' },
{ img_src: rootPath + 'test/normalbtn_h.png', name: 'name666', age: '13', like: 'type2' }
],
//数据长度
data_count: 5,
//刷新数据后停留在当前页面,不设置或者设为0则回到list顶部
on_page: 1
})

代码示例

Page({
build() {
const dataList = [
{ name: 'Amazfit T-Rex 2', size: 454 },
{ name: 'Amazfit GTR 3 Pro', size: 480 },
{ name: 'Amazfit GTR 3', size: 454 }
]

hmUI.createWidget(hmUI.widget.SCROLL_LIST, {
x: 0,
y: 120,
h: 300,
w: 480,
item_space: 20,
item_config: [
{
type_id: 1,
item_bg_color: 0xef5350,
item_bg_radius: 10,
text_view: [
{ x: 0, y: 0, w: 480, h: 80, key: 'name', color: 0xffffff, text_size: 20 },
{ x: 0, y: 80, w: 480, h: 40, key: 'size', color: 0xffffff }
],
text_view_count: 2,
item_height: 120
},
{
type_id: 2,
item_bg_color: 0xef5350,
item_bg_radius: 10,
text_view: [
{ x: 0, y: 0, w: 480, h: 80, key: 'name', color: 0x000000, text_size: 20 },
{ x: 0, y: 80, w: 480, h: 40, key: 'size', color: 0x000000 }
],
text_view_count: 2,
item_height: 120
}
],
item_config_count: 2,
data_array: dataList,
data_count: dataList.length,
item_click_func: (item, index) => {
console.log(`scrollListItemClick index=${index}`)
updateConfig()
},
data_type_config: [
{
start: 0,
end: 1,
type_id: 1
},
{
start: 1,
end: 2,
type_id: 2
}
],
data_type_config_count: 2
})

function updateConfig() {
scroll_list.setProperty(hmUI.prop.UPDATE_DATA, {
data_type_config: [
{
start: 0,
end: 2,
type_id: 1
},
],
data_type_config_count: 1,
data_array: dataList,
data_count: dataList.length,
on_page: 1
})
}
}
})