list开发指导
========

list是用来显示列表的组件，包含一系列相同宽度的列表项，适合连续、多行地呈现同类数据。具体用法请参考[list API](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-components-container-list)。

#### 创建list组件

在pages/index目录下的hml文件中创建一个list组件。

<!-- xxx.hml -->
<div class="container"> 
 <list>    
   <list-item class="listItem"></list-item>
   <list-item class="listItem"></list-item>
   <list-item class="listItem"></list-item>
   <list-item class="listItem"></list-item>
 </list>
</div>

/\* xxx.css \*/
.container {
  width:100%;
  height:100%;
  flex-direction: column;
  align-items: center;
  background-color: #F1F3F5;
}
.listItem{
  height: 20%;
  background-color:#d2e0e0;
  margin-top: 20px;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/95/v3/3oE8SztyQD6T4nDxQ7uaNw/zh-cn_image_0000002664207863.png?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=A89815D99AF798DCF548A28266E5A8DC2FF5901CE7A1BD1F98ABA33EB592CF3F)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f6/v3/BKjiNQFxRASazncejPp1Hg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=E2576DC04C359019093D488E2D1857D35DCC4CB714B7BE29C6896B8A25F9683D)

*   <list-item-group>是<list>的子组件，实现列表分组功能，不能再嵌套<list>，可以嵌套<list-item>。
    
*   <list-item>是<list>的子组件，展示列表的具体项。
    

#### 添加滚动条

设置scrollbar属性为on即可在屏幕右侧生成滚动条，实现长列表或者屏幕滚动等效果。

<!-- xxx.hml -->
<div class="container">
  <list class="listCss" scrollbar="on" >
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
 </list>
</div> 

/\* xxx.css \*/
.container {
  flex-direction: column;
  background-color: #F1F3F5;
}
.listItem{
  height: 20%;
  background-color:#d2e0e0;
  margin-top: 20px;
}
.listCss{
  height: 100%;
  scrollbar-color: #8e8b8b;
  scrollbar-width: 50px;
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/45/v3/Tr3B7Th-RVymoOP_oFu0zw/zh-cn_image_0000002664327923.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=9E2AA6AB8F258972B0845F4D8748BDC9A7D6DD27C779D7A49CCD4DE484876DD3)

#### 添加侧边索引栏

设置indexer属性为自定义索引时，索引栏会显示在列表右边界处，indexer属性设置为true，默认为字母索引表。

<!-- xxx.hml -->
<div class="container">   
  <list class="listCss"  indexer="{{\['#','1','2','3','4','5','6','7','8'\]}}" >  
    <list-item class="listItem"  section="#" ></list-item>   
  </list>
</div>

/\* xxx.css \*/
.container{
  flex-direction: column;
  background-color: #F1F3F5;
 }
.listCss{
  height: 100%;
  flex-direction: column;
  columns: 1
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/29/v3/q7XQBThhQAO25riqH1A6OQ/zh-cn_image_0000002633848814.png?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=D6B1877D5C0E16EAE4B8B1360351E220229417FC9C3F560DB7B0B403AB4914E1)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/4c/v3/G4_5vffHTkmWhnkjRPJpDQ/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=F96A1643A522C08265507C62C6FD79EC03D190AB615D23998B5940777213A70D)

*   indexer属性生效需要flex-direction属性配合设置为column，且columns属性设置为1。
    
*   indexer可以自定义索引表，自定义时"#"必须要存在。
    

#### 实现列表折叠和展开

为list组件添加groupcollapse和groupexpand事件实现列表的折叠和展开。

<!-- xxx.hml -->
<div class="doc-page">
  <list style="width: 100%;" id="mylist">
    <list-item-group for="listgroup in list" id="{{listgroup.value}}" ongroupcollapse="collapse" ongroupexpand="expand">
      <list-item type="item" style="background-color:#FFF0F5;height:95px;">
        <div class="item-group-child">
          <text>One---{{listgroup.value}}</text>
        </div>
      </list-item>
      <list-item type="item" style="background-color: #87CEFA;height:145px;" primary="true">
        <div class="item-group-child">
          <text>Primary---{{listgroup.value}}</text>
        </div>
      </list-item>
    </list-item-group>
  </list>
</div>

/\* xxx.css \*/
.doc-page {
  flex-direction: column;
  background-color: #F1F3F5;
}
.list-item {
  margin-top:30px;
}
.top-list-item {
  width:100%;
  background-color:#D4F2E7;
}
.item-group-child {
  justify-content: center;
  align-items: center;
  width:100%;
}

// xxx.js
import promptAction from '@ohos.promptAction';
export default {
  data: {
    direction: 'column',
    list: \[\]
  },
  onInit() {
    this.list = \[\]
    this.listAdd = \[\]
    for (var i = 1; i <= 2; i++) {
      var dataItem = {
        value: 'GROUP' + i,
      };
        this.list.push(dataItem);
    }
  },
  collapse(e) {
    promptAction.showToast({
      message: 'Close ' + e.groupid
    })
  },
  expand(e) {
    promptAction.showToast({
    message: 'Open ' + e.groupid
    })
  }
}

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/70/v3/ZQsOggVtQuy1MCaDmfkPEQ/zh-cn_image_0000002634008718.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=F75B0B454987413DF62E6A7AEA8697F110DFD10032B4E600BA233304A82B747C)

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f6/v3/_13UxQkTQQe--vCvR-oaHw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=F4A831A029371DA44C8EFE14F89633FB901501268AD699A4BEBD2BFCB0CADD56)

*   groupcollapse和groupexpand事件仅支持list-item-group组件使用。

#### 场景示例

在本场景中，开发者可以根据字母索引表查找对应联系人。

<!-- xxx.hml -->
<div class="doc-page"> 
  <text style="font-size: 35px; font-weight: 500; text-align: center; margin-top: 20px; margin-bottom: 20px;"> 
      <span>Contacts</span> 
  </text> 
  <list class="list" indexer="true"> 
    <list-item class="item" for="{{namelist}}" type="{{$item.section}}" section="{{$item.section}}"> 
      <div class="container"> 
        <div class="in-container"> 
          <text class="name">{{$item.name}}</text> 
          <text class="number">18888888888</text> 
        </div> 
      </div> 
    </list-item> 
    <list-item type="end" class="item"> 
      <div style="align-items:center;justify-content:center;width:750px;"> 
        <text style="text-align: center;">Total: 10</text> 
      </div> 
    </list-item> 
  </list> 
</div>

/\* xxx.css \*/
.doc-page {
  width: 100%;
  height: 100%;
  flex-direction: column;
  background-color: #F1F3F5;
}
.list {
  width: 100%;
  height: 90%;
  flex-grow: 1;
}
.item {
  height: 120px;
  padding-left: 10%;
  border-top: 1px solid #dcdcdc;
}
.name {
  color: #000000;
  font-size: 39px;
}
.number {
  color: black;
  font-size: 25px;
}
.container {
  flex-direction: row;
  align-items: center;
}
.in-container {
  flex-direction: column;
  justify-content: space-around;
}

// xxx.js
export default {
   data: {
     namelist:\[{
       name: 'Zoey',
       section:'Z'
     },{
       name: 'Quin',
       section:'Q'
     },{
       name:'Sam',
       section:'S'
     },{
       name:'Leo',
       section:'L'
     },{
       name:'Zach',
       section:'Z'
     },{
       name:'Wade',
       section:'W'
     },{
       name:'Zoe',
       section:'Z'
     },{
        name:'Warren',
        section:'W'
     },{
        name:'Kyle',
        section:'K'
     },{
       name:'Zaneta',
       section:'Z'
     }\]
   },
   onInit() {
   }
 }

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/1c/v3/DEPwklJiRJSHgcMj_D_H1Q/zh-cn_image_0000002664207865.gif?HW-CC-KV=V1&HW-CC-Date=20260709T132319Z&HW-CC-Expire=86400&HW-CC-Sign=E3C55A10726655FA2E111597F2103478A332B3B4B50A36FD9D17D0DC8E701990)