1# list开发指导 2 3<!--Kit: ArkUI--> 4<!--Subsystem: ArkUI--> 5<!--Owner: @yylong--> 6<!--Designer: @yylong--> 7<!--Tester: @liuzhenshuo--> 8<!--Adviser: @HelloCrease--> 9 10list是用来显示列表的组件,包含一系列相同宽度的列表项,适合连续、多行地呈现同类数据。具体用法请参考[list API](../reference/apis-arkui/arkui-js/js-components-container-list.md)。 11 12 13## 创建list组件 14 15在pages/index目录下的hml文件中创建一个list组件。 16 17```html 18<!-- xxx.hml --> 19<div class="container"> 20 <list> 21 <list-item class="listItem"></list-item> 22 <list-item class="listItem"></list-item> 23 <list-item class="listItem"></list-item> 24 <list-item class="listItem"></list-item> 25 </list> 26</div> 27``` 28 29```css 30/* xxx.css */ 31.container { 32 width:100%; 33 height:100%; 34 flex-direction: column; 35 align-items: center; 36 background-color: #F1F3F5; 37} 38.listItem{ 39 height: 20%; 40 background-color:#d2e0e0; 41 margin-top: 20px; 42} 43``` 44 45 46 47> **说明:** 48> - <list-item-group>是<list>的子组件,实现列表分组功能,不能再嵌套<list>,可以嵌套<list-item>。 49> 50> - <list-item>是<list>的子组件,展示列表的具体项。 51 52 53## 添加滚动条 54 55设置scrollbar属性为on即可在屏幕右侧生成滚动条,实现长列表或者屏幕滚动等效果。 56 57```html 58<!-- xxx.hml --> 59<div class="container"> 60 <list class="listCss" scrollbar="on" > 61 <list-item class="listItem"></list-item> 62 <list-item class="listItem"></list-item> 63 <list-item class="listItem"></list-item> 64 <list-item class="listItem"></list-item> 65 <list-item class="listItem"></list-item> 66 <list-item class="listItem"></list-item> 67 </list> 68</div> 69``` 70 71```css 72/* xxx.css */ 73.container { 74 flex-direction: column; 75 background-color: #F1F3F5; 76} 77.listItem{ 78 height: 20%; 79 background-color:#d2e0e0; 80 margin-top: 20px; 81} 82.listCss{ 83 height: 100%; 84 scrollbar-color: #8e8b8b; 85 scrollbar-width: 50px; 86} 87``` 88 89 90 91 92## 添加侧边索引栏 93 94设置indexer属性为自定义索引时,索引栏会显示在列表右边界处,indexer属性设置为true,默认为字母索引表。 95 96```html 97<!-- xxx.hml --> 98<div class="container"> 99 <list class="listCss" indexer="{{['#','1','2','3','4','5','6','7','8']}}" > 100 <list-item class="listItem" section="#" ></list-item> 101 </list> 102</div> 103``` 104 105```css 106/* xxx.css */ 107.container{ 108 flex-direction: column; 109 background-color: #F1F3F5; 110 } 111.listCss{ 112 height: 100%; 113 flex-direction: column; 114 columns: 1 115} 116``` 117 118 119 120> **说明:** 121> - indexer属性生效需要flex-direction属性配合设置为column,且columns属性设置为1。 122> 123> - indexer可以自定义索引表,自定义时"\#"必须要存在。 124 125 126## 实现列表折叠和展开 127 128为list组件添加groupcollapse和groupexpand事件实现列表的折叠和展开。 129 130```html 131<!-- xxx.hml --> 132<div class="doc-page"> 133 <list style="width: 100%;" id="mylist"> 134 <list-item-group for="listgroup in list" id="{{listgroup.value}}" ongroupcollapse="collapse" ongroupexpand="expand"> 135 <list-item type="item" style="background-color:#FFF0F5;height:95px;"> 136 <div class="item-group-child"> 137 <text>One---{{listgroup.value}}</text> 138 </div> 139 </list-item> 140 <list-item type="item" style="background-color: #87CEFA;height:145px;" primary="true"> 141 <div class="item-group-child"> 142 <text>Primary---{{listgroup.value}}</text> 143 </div> 144 </list-item> 145 </list-item-group> 146 </list> 147</div> 148``` 149 150```css 151/* xxx.css */ 152.doc-page { 153 flex-direction: column; 154 background-color: #F1F3F5; 155} 156.list-item { 157 margin-top:30px; 158} 159.top-list-item { 160 width:100%; 161 background-color:#D4F2E7; 162} 163.item-group-child { 164 justify-content: center; 165 align-items: center; 166 width:100%; 167} 168``` 169 170```js 171// xxx.js 172import promptAction from '@ohos.promptAction'; 173export default { 174 data: { 175 direction: 'column', 176 list: [] 177 }, 178 onInit() { 179 this.list = [] 180 this.listAdd = [] 181 for (var i = 1; i <= 2; i++) { 182 var dataItem = { 183 value: 'GROUP' + i, 184 }; 185 this.list.push(dataItem); 186 } 187 }, 188 collapse(e) { 189 promptAction.showToast({ 190 message: 'Close ' + e.groupid 191 }) 192 }, 193 expand(e) { 194 promptAction.showToast({ 195 message: 'Open ' + e.groupid 196 }) 197 } 198} 199``` 200 201 202 203> **说明:** 204> 205> - groupcollapse和groupexpand事件仅支持list-item-group组件使用。 206 207 208## 场景示例 209 210在本场景中,开发者可以根据字母索引表查找对应联系人。 211 212 213```html 214<!-- xxx.hml --> 215<div class="doc-page"> 216 <text style="font-size: 35px; font-weight: 500; text-align: center; margin-top: 20px; margin-bottom: 20px;"> 217 <span>Contacts</span> 218 </text> 219 <list class="list" indexer="true"> 220 <list-item class="item" for="{{namelist}}" type="{{$item.section}}" section="{{$item.section}}"> 221 <div class="container"> 222 <div class="in-container"> 223 <text class="name">{{$item.name}}</text> 224 <text class="number">18888888888</text> 225 </div> 226 </div> 227 </list-item> 228 <list-item type="end" class="item"> 229 <div style="align-items:center;justify-content:center;width:750px;"> 230 <text style="text-align: center;">Total: 10</text> 231 </div> 232 </list-item> 233 </list> 234</div> 235``` 236 237 238```css 239/* xxx.css */ 240.doc-page { 241 width: 100%; 242 height: 100%; 243 flex-direction: column; 244 background-color: #F1F3F5; 245} 246.list { 247 width: 100%; 248 height: 90%; 249 flex-grow: 1; 250} 251.item { 252 height: 120px; 253 padding-left: 10%; 254 border-top: 1px solid #dcdcdc; 255} 256.name { 257 color: #000000; 258 font-size: 39px; 259} 260.number { 261 color: black; 262 font-size: 25px; 263} 264.container { 265 flex-direction: row; 266 align-items: center; 267} 268.in-container { 269 flex-direction: column; 270 justify-content: space-around; 271} 272``` 273 274 275```js 276// xxx.js 277export default { 278 data: { 279 namelist:[{ 280 name: 'Zoey', 281 section:'Z' 282 },{ 283 name: 'Quin', 284 section:'Q' 285 },{ 286 name:'Sam', 287 section:'S' 288 },{ 289 name:'Leo', 290 section:'L' 291 },{ 292 name:'Zach', 293 section:'Z' 294 },{ 295 name:'Wade', 296 section:'W' 297 },{ 298 name:'Zoe', 299 section:'Z' 300 },{ 301 name:'Warren', 302 section:'W' 303 },{ 304 name:'Kyle', 305 section:'K' 306 },{ 307 name:'Zaneta', 308 section:'Z' 309 }] 310 }, 311 onInit() { 312 } 313 } 314``` 315 316 317