从微信小程序到鸿蒙JS开发【04】-list组件

开发
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz

[[381248]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

1、可滚动区域

在许多场景中,页面会有一块区域是可滚动的,比如这样一个简单的每日新闻模块:

从微信小程序到鸿蒙js开发【04】——list组件
从微信小程序到鸿蒙js开发【04】——list组件

上面的新闻类型是一块可横向滚动的区域,下方新闻列表是一块可竖向滚动的区域。在微信小程序中,使用scroll-view组件即可实现。那么在鸿蒙js组件中,想要实现可滚动的区域,则是使用list组件。list仅支持竖向滚动,横向滚动要用tabs,将在下篇博客讲解。

2、list + list-item

这里以本地新闻模块为例,数据请求自天行数据接口(https://www.tianapi.com/apiview/154)。

从微信小程序到鸿蒙js开发【04】——list组件

上方为一个搜索框,下方是新闻列表。搜索框给了固定高度,那么怎样让新闻列表能够占满屏幕剩余部分呢?只需将父容器设置flex布局,list设置flex: 1即可。list下直接放list-item,在总高度超出list的高度后,即可上下滚动。

hml:

  1. <!-- 本地新闻 --> 
  2.  <div> 
  3.      <div class="searchView"
  4.          <image src="{{ searchIcon }}"></image> 
  5.          <input placeholder="搜你想看的"></input> 
  6.      </div> 
  7.      <list class="localView"
  8.          <block for="{{ localNews }}"
  9.              <list-item class="newsItem"
  10.                  <div class="newsContent"
  11.                      <text> 
  12.                          {{ $item.title }} 
  13.                      </text> 
  14.                      <div class="newsDesc"
  15.                          <text> 
  16.                              {{ $item.source }} 
  17.                          </text> 
  18.                          <text> 
  19.                              {{ $item.ctime }} 
  20.                          </text> 
  21.                      </div> 
  22.                  </div> 
  23.              </list-item> 
  24.          </block> 
  25.      </list> 
  26.  </div> 
  27.  <!-- 本地新闻end --> 

css:

  1. /*本地新闻*/ 
  2. .searchView { 
  3.     width: 100%; 
  4.     height: 140px; 
  5.     background-color: #f0f0f0; 
  6.     display: flex; 
  7.     align-items: center; 
  8. .searchView>image { 
  9.     margin: 0 40px 0 40px; 
  10.     height: 60px; 
  11.     width: 60px; 
  12. .searchView>input { 
  13.     margin-right: 40px; 
  14. .localView { 
  15.     width: 100%; 
  16.     flex: 1; 
  17.     display: flex; 
  18.     flex-direction: column
  19. .localContent { 
  20.     margin-left: 20px; 
  21. .newsItem { 
  22.     width: 100%; 
  23.     height: 240px; 
  24.     border-bottom: 1px solid #bbbbbb; 
  25.     display: flex; 
  26.     align-items: center; 
  27. .newsContent { 
  28.     display: flex; 
  29.     flex-direction: column
  30.     margin-right: 20px; 
  31.     margin-left: 20px; 
  32. .newsContent>text { 
  33.     margin-top: 20px; 
  34.     height: 140px; 
  35.     font-size: 34px; 
  36.     color: #333333; 
  37. .newsDesc { 
  38.     height: 60px; 
  39.     line-height: 60px; 
  40.     display: flex; 
  41.     justify-content: space-between
  42. .newsDesc>text { 
  43.     font-size: 28px; 
  44.     color: #777777; 

 js:

  1. searchLocalNews() { 
  2.      let url = 'http://api.tianapi.com/areanews/index?key=xxxx&areaname=江苏'
  3.      if (this.searchWord) { 
  4.          url = url + '&word' + this.searchWord; 
  5.      } 
  6.      fetch.fetch({ 
  7.          url: url, 
  8.          responseType: 'json'
  9.          success: res => { 
  10.              let data = JSON.parse(res.data); 
  11.              this.localNews = data.newslist; 
  12.          } 
  13.      }) 
  14.  }, 

 新闻列表可滚动,且不会影响搜索框的位置。


3、list + list-item-group + list-item

list组件的子元素还可以是list-item-group,顾名思义应是分组列表项,list-item作为list-item-group的子元素。随便写一点看一看:

  1. <div> 
  2.         <list class="manageList"
  3.             <list-item-group class="list-item-group"
  4.                 <list-item class="list-item"
  5.                     <text> 
  6.                         <span>分组1 子项1</span> 
  7.                     </text> 
  8.                 </list-item> 
  9.                 <list-item class="list-item"
  10.                     <text> 
  11.                         <span>分组1 子项2</span> 
  12.                     </text> 
  13.                 </list-item> 
  14.                 <list-item class="list-item"
  15.                     <text> 
  16.                         <span>分组1 子项3</span> 
  17.                     </text> 
  18.                 </list-item> 
  19.             </list-item-group
  20.             <list-item-group class="list-item-group"
  21.                 <list-item class="list-item"
  22.                     <text> 
  23.                         <span>分组2 子项1</span> 
  24.                     </text> 
  25.                 </list-item> 
  26.                 <list-item class="list-item"
  27.                     <text> 
  28.                         <span>分组2 子项2</span> 
  29.                     </text> 
  30.                 </list-item> 
  31.                 <list-item class="list-item"
  32.                     <text> 
  33.                         <span>分组2 子项3</span> 
  34.                     </text> 
  35.                 </list-item> 
  36.             </list-item-group
  37.         </list> 
  38.     </div> 

  1. .manageList{ 
  2.     height: 100%; 
  3.     width: 100%; 
  4. .list-item-group
  5.     width: 100%; 
  6.     height: 450px; 
  7. .list-item{ 
  8.     width: 100%; 
  9.     height: 150px; 
  10.     display: flex; 
  11.     justify-content: center; 
  12.     align-items: center; 
  13.     border-bottom: 1px solid gray; 
  14. .list-item>text{ 
  15.     line-height: 100px; 

  

 

可以看出,list-item-group是可折叠的列表分组,且默认是折叠的。点击右侧小箭头可展开列表,如果list-item-group给了高度,则折叠和展开后这一块区域的高度不变。在折叠时,展示第一个list-item的内容。

那么如果每一个list-item-group中list-item数目不固定,在展开后的布局就会很难看。如下:


其实不定义list-item-group的高度即可,折叠高度为list-item的高度,展开后高度自适应增长,超出list高度可以滚动,功能还是很强大的。更改css后的效果如下:


 

这种分组的列表,可以制作一个简单的后台管理系统菜单界面。这里我将菜单数据文件、图片文件放入nginx服务器的目录中,再通过内网穿透访问资源。注意数据的格式,list-item-group和list-item之间存在父级标签关系,故数据中也应存在父级关系。list-item-group展示的内容是其下第一个list-item,这里用一个两重for循环实现:



manage.json:

  1.     "manageList": [ 
  2.         { 
  3.             "name""组织管理"
  4.             "icon""http://milkytea.free.idcfengye.com/images/christools/icon/org.png"
  5.             "subList": [ 
  6.                 { 
  7.                     "name""查询组织"
  8.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/search.png" 
  9.                 }, 
  10.                 { 
  11.                     "name""添加组织"
  12.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  13.                 }, 
  14.                 { 
  15.                     "name""删除组织"
  16.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/delete.png" 
  17.                 } 
  18.             ] 
  19.         }, 
  20.         { 
  21.             "name""人员管理"
  22.             "icon""http://milkytea.free.idcfengye.com/images/christools/icon/person.png"
  23.             "subList": [ 
  24.                 { 
  25.                     "name""查询人员"
  26.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/search.png" 
  27.                 }, 
  28.                 { 
  29.                     "name""添加人员"
  30.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  31.                 }, 
  32.                 { 
  33.                     "name""批量导入人员"
  34.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  35.                 }, 
  36.                 { 
  37.                     "name""删除人员"
  38.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/delete.png" 
  39.                 }, 
  40.                 { 
  41.                     "name""修改人员"
  42.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/update.png" 
  43.                 } 
  44.             ] 
  45.         }, 
  46.         { 
  47.             "name""卡片管理"
  48.             "icon""http://milkytea.free.idcfengye.com/images/christools/icon/card.png"
  49.             "subList": [ 
  50.                 { 
  51.                     "name""开卡"
  52.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/add.png" 
  53.                 }, 
  54.                 { 
  55.                     "name""退卡"
  56.                     "icon""http://milkytea.free.idcfengye.com/images/christools/icon/delete.png" 
  57.                 } 
  58.             ] 
  59.         } 
  60.     ] 

 hml:

  1. <!-- 管理 --> 
  2.      <div> 
  3.          <list class="manageList"
  4.              <block for="{{ manageList }}"
  5.                  <list-item-group class="list-item-group"
  6.                      <list-item class="list-item"
  7.                          <image src="{{ $item.icon }}"></image> 
  8.                          <text>{{ $item.name }}</text> 
  9.                      </list-item> 
  10.                      <block for="{{ (index, value) in $item.subList }}"
  11.                          <list-item class="list-item"
  12.                              <image src="{{ value.icon }}"></image> 
  13.                              <text>{{ value.name }}</text> 
  14.                          </list-item> 
  15.                      </block> 
  16.                  </list-item-group
  17.              </block> 
  18.          </list> 
  19.      </div> 
  20.      <!-- 管理end --> 

js:

  1. getManageList() { 
  2.        let url = "http://milkytea.free.idcfengye.com/text/manage.json"
  3.        fetch.fetch({ 
  4.            url: url, 
  5.            responseType: 'json'
  6.            success: res => { 
  7.                let data = JSON.parse(res.data); 
  8.                this.manageList = data.manageList; 
  9.            } 
  10.        }) 
  11.    } 

 想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-03-02 09:29:29

鸿蒙HarmonyOS应用开发

2021-02-23 09:52:42

鸿蒙HarmonyOS应用开发

2021-02-23 12:25:26

鸿蒙HarmonyOS应用开发

2021-02-25 15:13:08

鸿蒙HarmonyOS应用开发

2021-02-23 12:23:57

鸿蒙HarmonyOS应用开发

2021-02-21 11:09:18

鸿蒙HarmonyOS应用开发

2021-02-22 14:56:55

鸿蒙HarmonyOS应用开发

2021-02-25 10:01:19

鸿蒙HarmonyOS应用开发

2021-02-04 13:49:41

鸿蒙HarmonyOS应用开发

2021-02-05 09:46:16

鸿蒙HarmonyOSjs开发

2021-02-24 09:36:03

鸿蒙CSS应用开发

2021-02-07 09:17:24

鸿蒙HarmonyOS应用开发

2021-01-22 17:23:32

鸿蒙HarmonyOS应用

2017-05-08 15:03:07

微信小程序开发实战

2016-09-27 16:38:24

JavaScript微信Web

2016-11-04 10:49:48

微信小程序

2016-09-28 18:10:59

微信程序MINA

2016-09-27 20:36:23

微信HttpWeb

2016-11-04 10:30:17

微信小程序

2023-01-05 09:01:05

UI组件库微信
点赞
收藏

51CTO技术栈公众号