目录

list

Overview

Functions as a list container.

Application Scope

Item

Description

Applicable devices

Mobile phones, tablets, Vision products, and telematics devices

Applicable regions

Global

Subelements

Only the list-item subelement is supported.

Attributes

In addition to general attributes, the attribute listed in the following table is supported.

Attribute

Type

Default Value

Mandatory

Description

scrollpage

boolean

false

No

Indicates whether to remove the non-list part together with the list on top of the list from the visible area. Enabling this attribute will compromise list rendering performance.

CAUTION:

The scrollpage attribute is not supported if the list is horizontal.

Styles

In addition to common styles, the styles listed in the following table are supported.

If only the width of list is set, retaining the default value 0 for the flex-grow style and setting it to 0 manually will have different effect.

Style

Type

Default Value

Mandatory

Description

flex-direction

column | row | row-reverse (1035+) | column-reverse (1035+)

column

No

Sets the main axis direction of the flex container and specifies how the flex item is placed in the flex container. In the list element, the default value of this style is column. In other elements, the default value is row.

columns

number

1

No

Number of displayed columns.

layout-type (1070+)

grid | stagger

grid

No

  • grid: grid style.
  • stagger: waterfall style. In this mode, the height of each list-item can be customized.

Events

In addition to common events, the events listed in the following table are supported.

Event

Parameter

Description

scroll

{scrollX:scrollXValue, scrollY:scrollYValue, scrollState:stateValue 1010+}

A list is scrolling.

  • The value 0 of stateValue indicates that a list is not scrolling.
  • The value 1 of stateValue indicates that a user is scrolling a list with gestures.
  • The value 2 of stateValue indicates that a list is scrolling and a user already took fingers off the screen.

scrollbottom

-

A list is scrolled to the bottom.

scrolltop

-

A list is scrolled to the top.

scrollend (1035+)

-

A list stops scrolling.

scrolltouchup (1035+)

-

A list is scrolling inertially after the user takes fingers off the screen.

Methods

Method

Parameter

Description

scrollTo

object

Scrolls a list to the position of a specified item.

scrollBy

object

Scrolls a list a certain distance away. (This function is supported in 1070 and later versions.)

CAUTION:

Vision products support only the dx, dy, and smooth parameters.

scrollTo parameters

Parameter

Type

Mandatory

Default Value

Description

index

number

Yes

-

Scrolling destination.

smooth (1060+)

boolean

No

false

Indicates whether to enable smooth scrolling.

behavior (1065+)

string

No

auto

This parameter functions the same as the smooth parameter. The value can be smooth, instant, or auto. The default value is auto, which functions the same as instant.

scrollBy parameters

Parameter

Type

Mandatory

Default Value

Description

dx

int

No

0

Horizontal scrolling distance from the current position, in px.

dy

int

No

0

Vertical scrolling distance from the current position, in px.

smooth

boolean

No

true

Indicates whether to scroll a list smoothly.

Or

Parameter

Type

Mandatory

Default Value

Description

left

number

No

0

Horizontal scrolling distance from the current position, in px. If the value is positive, the list scrolls to the left. If the value is negative, the list scrolls to the right. This parameter does not take effect when flex-direction is set to column or column-reverse.

top

number

No

0

Vertical scrolling distance from the current position, in px. If the value is positive, the list scrolls upwards. If the value is negative, the list scrolls downwards. This parameter does not take effect when flex-direction is set to row or row-reverse.

behavior

string

No

auto

Indicates whether to enable smooth scrolling. The value can be smooth, instant, or auto. The default value is auto, which functions the same as instant.

Sample Code

<template>
  <div class="container">
    <div class="page-title-wrap">
      <text class="page-title">{{componentName}}</text>
    </div>

    <div class="nav">
      <text class="nav-item {{listClass}}"
        onclick="showListMethod">List element method</text>
      <text class="nav-item {{arrayClass}}"
        onclick="showArrayMethod">Array object method</text>
    </div>

    <!-- List component method -->
    <div class="btn-wrap-column" show="{{showListBtn}}">
      <input type="button" class="btn" name="listmethod" value="Go back to the first item"
        onclick="goIndex" />
      <input type="button" class="btn" name="listmethod" value="Scroll to the right by 200 px"
        onclick="scrollBy" />
    </div>

    <!-- Array object method -->
    <div class="btn-wrap-row" show="{{!showListBtn}}">
      <input type="button" class="btn-little" onclick="clickPush" value="push"></input>
      <input type="button" class="btn-little" onclick="clickPop" value="pop"></input>
      <input type="button" class="btn-little" onclick="clickShift" value="shift"></input>
      <input type="button" class="btn-little" onclick="clickUnshift" value="unshift"></input>
    </div>

    <div class="btn-wrap-row" show="{{!showListBtn}}">
      <input type="button" class="btn-little" onclick="clickSplice" value="splice"></input>
      <input type="button" class="btn-little" onclick="clickSort" value="sort"></input>
      <input type="button" class="btn-little" onclick="clickReverse" value="reverse"></input>
    </div>
    <div class="nav">
      <text class="nav-item {{gridClass}}" onclick="showGridLayout">Grid layout</text>
      <text class="nav-item {{staggerClass}}"
        onclick="showStaggerLayout">Masonry layout</text>
    </div>

    <!-- List -->
    <list show="{{showGridLayout}}" class="list grid" onclick="listClick" onlongpress="listLongPress"
      onscrollbottom="scrollbottom" onscrolltop="scrolltop" onscroll="scroll" onscrollend="scrollend"
      onscrolltouchup="scrolltouchup" id="list" scrollpage="{{scrollPage}}">
      <!-- List elements, when the attribute type values are the same, you need to ensure that the rendered view layout is also the same, use show instead of if to ensure that the layout view is unchanged -->
      <list-item type="listItem" class="item item-color" onlongpress="listItemLongPress" onclick="listItemClick"
        for="{{listData}}">
        <text onclick="textClick" class="txt">{{$item}}--{{$idx}}</text>
      </list-item>

      <!-- Loading More  -->
      <list-item type="loadMore" class="load-more" if="{{loadMore}}">
        <progress type="circular"></progress>
        <text>More...</text>
      </list-item>
    </list>

    <list show="{{!showGridLayout}}" class="list stagger" onclick="listClick" onlongpress="listLongPress"
      onscrollbottom="scrollbottom" onscrolltop="scrolltop" onscroll="scroll" onscrollend="scrollend"
      onscrolltouchup="scrolltouchup" id="list-stagger" scrollpage="{{scrollPage}}">
      <!-- List elements, when the attribute type values are the same, you need to ensure that the rendered view layout is also the same, use show instead of if to ensure that the layout view is unchanged -->
      <list-item style="height: {{$idx % 10}}00px" type="listItem" class="item item-color"
        onlongpress="listItemLongPress" onclick="listItemClick" for="{{listData}}">
        <text onclick="textClick" class="txt">{{$item}}--{{$idx}}</text>
      </list-item>

      <!-- Loading More -->
      <list-item type="loadMore" class="load-more" if="{{loadMore}}">
        <progress type="circular"></progress>
        <text>More...</text>
      </list-item>
    </list>
  </div>
</template>
<style>
  .list {
    height: 800px;
    padding-left: 60px;
    padding-right: 60px;
    columns: 4;
    border-color: #FF0000;
    border-width: 5px;
  }

  .grid {
    flex-direction: row-reverse;
  }

  .stagger {
    flex-direction: column;
  }

  .container{
  flex: 1;
  flex-direction: column;
}

.page-title-wrap {
  padding-top: 50px;
  padding-bottom: 80px;
  justify-content: center;
}
.page-title{
  padding-top: 30px;
  padding-bottom: 30px;
  padding-left: 40px;
  padding-right: 40px;
  border-color: #bbbbbb;
  color: #bbbbbb;
  border-bottom-width: 2px;
}

.btn {
  height: 80px;
  text-align: center;
  border-radius: 5px;
  margin-right: 60px;
  margin-left: 60px;
  margin-bottom: 50px;
  color: #ffffff;
  font-size: 30px;
  background-color: #0faeff;
}

  .item {
    height: 150px;
    align-items: flex-start;
    margin-bottom: 15px;
    border-color: #9400D3;
    border-width: 5px;
    margin-right: 20px;
  }

  .load-more {
    justify-content: center;
    align-items: center;
    height: 100px;
    border-color: #bbbbbb;
    border-bottom-width: 1px;
  }

  .btn-wrap-column {
    flex-direction: column;
    margin-right: 60px;
    margin-left: 60px;
  }

  .btn-wrap-row {
    margin-right: 60px;
    margin-left: 45px;
    margin-bottom: 30px;
  }

  .btn-little {
    flex: 1;
    height: 80px;
    margin-left: 15px;
    border-radius: 5px;
    color: #ffffff;
    font-size: 30px;
    text-align: center;
    background-color: #0faeff;
  }

  .item-color {
    background-color: #f76160;
  }

  .nav {
    padding-left: 60px;
    padding-right: 60px;
    padding-bottom: 30px;
  }

  .nav-item {
    flex: 1;
    padding-bottom: 30px;
    border-bottom-width: 5px;
    border-color: #fbf9fe;
    font-size: 35px;
    color: #666666;
    text-align: center;
  }

  .selected {
    border-color: #0faeff;
  }
</style>
<script>
  import prompt from '@system.prompt'

  export default {
    data: {
      componentName: 'list',
      loadMore: true,
      listAdd: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
      listData: [],
      scrollPage: false,
      showListBtn: true,
      showGridLayout: true,
      listClass: 'selected',
      arrayClass: '',
      gridClass: 'selected',
      staggerClass: '',
    },
    onInit: function () {
      this.$page.setTitleBar({ text: 'list' })
      // Initialize list data
      this.listData = [].concat(this.listAdd)
    },
    showListMethod: function () {
      if (!this.showListBtn) {
        this.showListBtn = true
        this.listClass = 'selected'
        this.arrayClass = ''
      }
    },
    showArrayMethod: function () {
      if (this.showListBtn) {
        this.showListBtn = false
        this.listClass = ''
        this.arrayClass = 'selected'
      }
    },
    showStaggerLayout: function () {
      if (this.showGridLayout) {
        this.showGridLayout = false
        this.gridClass = ''
        this.staggerClass = 'selected'
      }
    },
    showGridLayout: function () {
      if (!this.showGridLayout) {
        this.showGridLayout = true
        this.gridClass = 'selected'
        this.staggerClass = ''
      }
    },
    scrollbottom: function () {
      prompt.showToast({
        message: "Bottom reached.'
      })
      // Load next page
      var that = this
      var renderData = [].concat(that.listData, that.listAdd)
      setTimeout(function () {
        that.listData = renderData
      }, 1000)
    },
    scrolltop: function () {
      prompt.showToast({
        message:"Top reached."
      })
    },
    scrollend: function () {
      prompt.showToast({
        message: "Stop scrolling."

      })
      console.error(this.$t('message.component.list.scrollStoped'))
    },
    scrolltouchup: function () {
      prompt.showToast({
        message: "The finger leaves the screen."
      })

    },

    // monitoring during sliding
    scroll: function (e) {
      let msg =  " Scrolling "+ '.scrollX:' + e.scrollX
        + ' .scrollY:' + e.scrollY
        + ' .scrollState:' + e.scrollState
      console.info(msg)
    },
    goIndex: function () {
      if (this.showGridLayout) {
        this.$element('list').scrollTo({ index: 0, behavior: 'smooth' })
      } else {
        this.$element('list-stagger').scrollTo({ index: 0, behavior: 'smooth' })
      }
    },
    scrollBy: function () {
      console.log('list.scrollBy: left=200, behavior= smooth')
      if (this.showGridLayout) {
        this.$element('list').scrollBy({ left: 200, behavior: 'smooth' })
      } else {
        this.$element('list-stagger').scrollBy({ top: 200, behavior: 'smooth' })
      }
    },
    clickPush: function () {
      this.listData.push('push')
      prompt.showToast({
        message: "Add an element to the end of an array."
      })
    },
    clickPop: function () {
      prompt.showToast({
message: "Delete the last element of an array."+ JSON.stringify(this.listData.pop())
      })
    },
    clickShift: function () {
      prompt.showToast({
        message: "Delete the first element of an array."+ JSON.stringify(this.listData.shift())
      })
    },
    clickUnshift: function () {
      this.listData.unshift('unshift')
      prompt.showToast({
        message: "Add an unshift element to the end of an array."
      })
    },
    clickSplice: function () {
      prompt.showToast({
        message: "Delete the first element "+ JSON.stringify(this.listData.splice(0, 1, 'splice')[0]) + ',' + "and add a splice element"
      })
    },
    clickSort: function () {
      this.listData.sort()
      prompt.showToast({
        message: 'sort'
      })
    },
    clickReverse: function () {
      this.listData.reverse()
      prompt.showToast({
        message: 'reverse'
      })
    },
    listItemClick: function (e) {
      e.stopPropagation()
      console.info('List Item is clicked.')
      prompt.showToast({
        message: 'List Item is clicked.'
      })
    },
    listClick: function (e) {
      e.stopPropagation()
      console.info('List is clicked.')
      prompt.showToast({
        message: 'List is clicked.'
      })
    },
    textClick: function (e) {
      e.stopPropagation()
      console.info('Text is clicked.')
      prompt.showToast({
        message: 'Text is clicked.'
      })
    },
    listLongPress: function (e) {
      e.stopPropagation()
      console.info('List is long pressed.')
      prompt.showToast({
        message: 'List is long pressed.'
      })
    },
    listItemLongPress: function (e) {
      e.stopPropagation()
      console.info('List Item is long pressed.')
      prompt.showToast({
        message: 'List Item is long pressed.'
      })
    },
  }
</script>

The following figures show the final effect.

Version Change History

Version

Date

Description

1070

2020-05-19

  • Added the scrollBy attribute to control list to scroll from the current position.
  • Added the behavior attribute to the scrollTo method to determine whether to perform smooth scrolling.
  • Added the layout-type style to control whether subelements are displayed in grid or waterfall mode.

1060

2019-12-10

Added the smooth attribute to scrollTo of list so that the page can be smoothly scrolled.

1040

2019-06-24

  • Added row-reverse and column-reverse for the flex-direction style.
  • Added the scrollend and touchup events.

1010

2018-04-20

Added scrollState to the scroll event.

Tips

Sometimes, the scrollto method may fail to scroll a list to the specified location. The possible cause is that the element rendering has not finished. You are advised to call the method 50 milliseconds later.

Links

[h2]FAQs

[h2]Cases