Skip to main content
Version: v3+

SCROLL_LIST

Start from API_LEVEL 2.0. Please refer to API_LEVEL.

scroll_list_sample

Create a list area with sliding support, where each list item can contain images and text, and supports horizontal sliding.

Create UI widget

import { createWidget, widget } from '@zos/ui'

const scrollList = createWidget(widget.SCROLL_LIST, Param)

Type

Param: object

PropertiesDescriptionRequiredTypeAPI_LEVEL
xThe x-coordinate of the widgetYESnumber2.0
yThe y-coordinate of the widgetYESnumber2.0
wWidth of the widgetYESnumber2.0
hHeight of the widgetYESnumber2.0
item_spaceSpace between itemsNOnumber2.0
item_configItem type configuration, see ItemConfigYESArray<ItemConfig>2.0
item_config_countLength of the item_config arrayYESnumber2.0
data_arrayData arrayYESDataArray2.0
data_countLength of the data arrayYESnumber2.0
item_click_funcItem click callback function, see ItemClickFuncNOItemClickFunc2.0
data_type_configItem index type configuration array, see DataTypeConfigNOArray<DataTypeConfig>2.0
data_type_config_countLength of the item index type configuration arrayNOnumber2.0
on_pageUsed when updating data, set to 1 to keep the list at current position after update, otherwise returns to list topNOnumber2.0
snap_to_centerWhether the list should snap to the center height of SCROLL_LISTNOboolean2.0
item_focus_change_funcList sliding focus change callback function, see ItemFocusChangeFuncNOItemFocusChangeFunc2.0
item_enable_horizon_dragWhether items can be dragged horizontallyNOboolean2.0
item_drag_max_distanceMaximum horizontal drag distance, positive values for left drag, negative for right dragNOnumber2.0
snap_typeSet snap mode (see snap_type snap mode)NOnumber4.0
item_common_focusWhether to show common focus (effective in key mode)NOboolean4.0
item_key_focus_change_funcKey event listener callback in key modeNOfunction4.0
enable_scroll_barCreate page indicator (arcScrollBar)NOboolean4.0
view_indexSet list item to visible area (Note: Round screen: center screen, Square screen: top of screen)NOnumber4.0

ItemConfig: object

PropertiesDescriptionRequiredType
type_idCurrent item type ID, optional when item_config_count is 0, required otherwiseNOnumber
item_heightItem heightYESnumber
item_bg_colorItem background colorYESnumber
item_bg_radiusItem background corner radiusYESnumber
text_viewArray of textView structures, each item is a textView, see explanation belowNOArray<TextView>
text_view_countLength of text_view arrayNOnumber
image_viewArray of imageView, each item is an imageView, see explanation belowNOArray<ImageView>
image_view_countLength of image_view arrayNOnumber

TextView: object

PropertiesDescriptionRequiredType
xThe x-coordinate, relative coordinateYESnumber
yThe y-coordinate, relative coordinateYESnumber
wWidget widthYESnumber
hWidget heightYESnumber
colorText colorNOnumber
text_sizeFont sizeNOnumber
keyData binding key, see examples and data_array description for detailsYESstring
actionWhether to respond to click events, after response, the corresponding data key can be captured in item_click_func, default falseNOboolean
const text_view = [
{ x: 100, y: 0, w: 100, h: 20, key: 'name', color: 0xffffff, action: true },
{ x: 0, y: 30, w: 100, h: 100, key: 'age', color: 0xffffff, text_size: 20 }
]

ImageView: object

PropertiesDescriptionRequiredType
xThe x-coordinate, relative coordinateYESnumber
yThe y-coordinate, relative coordinateYESnumber
wWidget widthYESnumber
hWidget heightYESnumber
keyData binding key, see examples and data_array description for detailsYESstring
actionWhether to respond to click events, after response, the corresponding data key can be captured in item_click_func, default falseNOboolean
// Each structure in the array is an imageView
const image_view = [{ x: 0, y: 0, w: 20, h: 20, key: 'img_src', action: true }]

### data_array

Data arrays, TextView and ImageView take values from each data_array object based on property names (configured via `key`) and render them to the view based on the corresponding configuration.

```js
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' }
]

DataTypeConfig: object

Set the type of list index item.

Each index item uses the configuration of item_config[0] by default.

PropertiesDescriptionRequiredType
startStarting indexYESnumber
endEnding indexYESnumber
type_idThe type_id corresponding to the type configuration in item_configYESnumber

start and end form a closed interval [start, end]

{
data_type_config:[
// Represents that data entries from index 0 to 2 use the style configuration with type_id 2
{
start: 0,
end: 2,
type_id: 2,
},
{
start: 3,
end: 10,
type_id: 1,
},
],
data_type_config_count:2
}

ItemClickFunc

(list: ScrollList, index: number, data_key: string) => void
ParametersDescriptionType
listSCROLL_LIST widgetany
indexClicked item indexnumber
data_keyClicked data key name, can locate the clicked area through keystring

ItemFocusChangeFunc

(list: ScrollList, index: number, focus: boolean) => void
ParametersDescriptionType
listSCROLL_LIST widgetany
indexItem indexnumber
focusWhether the item is in focus stateboolean

snap_type Snap Mode

Snap Mode Enum ValueDescription
SCROLL_LIST.snap_type.SNAPCENTER_ALLCenter snap
SCROLL_LIST.snap_type.SNAPCENTER_EXCEPTTITLECenter snap except for title
SCROLL_LIST.snap_type.SNAP_TOPTop snap
SCROLL_LIST.snap_type.SNAP_BOTTOMBottom snap
import { SCROLL_LIST } from '@zos/ui'

console.log(SCROLL_LIST.snap_type.SNAPCENTER_ALL)

Refresh Data

import { createWidget, widget, prop } from '@zos/ui'

const scrollList = createWidget(widget.SCROLL_LIST, Param)
scrollList.setProperty(prop.UPDATE_DATA, {
// Reset configuration information
data_type_config: [
{
start: 0,
end: 2,
type_id: 2
}
],
// Configuration information length
data_type_config_count: 1,
// New data
data_array: [
{ img_src: rootPath + 'test/normalbtn_h.png', name: '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 length
data_count: 5,
// Stay on current page after data refresh, if not set or set to 0, it will return to the top of the list
on_page: 1
})

Update/Delete Single Item

// Update a specific data
scrollList.setProperty(prop.UPDATE_ITEM, {
index: gScrollListSelectIndex,
item_data: dataList2[gScrollListSelectIndex]
})
// Delete a specific data
list.setProperty(prop.DELETE_ITEM, { index: delete_index })

Control Horizontal Sliding Parameters of Single Item

scrollList.setProperty(prop.MOVE_ITEM, {
start: 0, // Start row
end: 0, // End row
item_enable_horizon_drag: false, // Whether horizontal sliding is enabled
item_drag_max_distance: -200 // Horizontal sliding distance, only takes effect when enabled
})

Property Access Support List

属性名setPropertygetPropertysettergetter
xNYNY
yNYNY
wNYNY
hNYNY
item_spaceNNNN
item_configNNNN
item_config_countNNNN
data_arrayNNNN
data_countNNNN
item_click_funcNNNN
data_type_configNNNN
data_type_config_countNNNN
on_pageNNNN
snap_to_centerNNNN
item_focus_change_funcNNNN
item_enable_horizon_dragNNNN
item_drag_max_distanceNNNN
snap_typeNNNN
item_common_focusNNNN
item_key_focus_change_funcNNNN
enable_scroll_barNNNN
view_indexNNNN

代码示例

import { createWidget, widget, prop } from '@zos/ui'

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

const scrollList = createWidget(widget.SCROLL_LIST, {
x: 0,
y: 120,
h: 300,
w: 480,
item_space: 20,
snap_to_center: true,
item_enable_horizon_drag: true,
item_drag_max_distance: -120,
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,
image_view: [{ x: 492, y: 28, w: 64, h: 64, key: 'del_img', action: true }],
image_view_count: 1,
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,
image_view: [{ x: 492, y: 28, w: 64, h: 64, key: 'del_img', action: true }],
image_view_count: 1,
item_height: 120
}
],
item_config_count: 2,
data_array: dataList,
data_count: dataList.length,
item_focus_change_func: (list, index, focus) => {
console.log('scrollListFocusChange index=' + index)
console.log('scrollListFocusChange focus=' + focus)
},
item_click_func: (item, index, data_key) => {
console.log(`scrollListItemClick index=${index}`)
if (data_key === 'del_img') {
scrollList.setProperty(prop.DELETE_ITEM, { index })
dataList.splice(index, 1)
} else {
updateConfig()
}
},
data_type_config: [
{
start: 0,
end: 1,
type_id: 1
},
{
start: 1,
end: 2,
type_id: 2
}
],
data_type_config_count: 2,
snap_to_center: true,
item_enable_horizon_drag: true,
item_drag_max_distance: -112
})

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