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

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

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

https://harmonyos./#zz

1、可滚动区域

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

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

2、list + list-item

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

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

hml:

 
 
 
 
  1.  
  2.      
  3.          
  4.          
  5.      
  •      
  •          
  •              
  •                  
  •                      
  •                          {{ $item.title }}
  •                      
  •                      
  •                          
  •                              {{ $item.source }}
  •                          
  •                          
  •                              {{ $item.ctime }}
  •                          
  •                      
  •                  
  •              
  •          
  •      
  •  
  •  
  • css:

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

     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.         
    2.             
    3.                 
    4.                     
    5.                         分组1 子项1
    6.                     
    7.                 
    8.                 
    9.                     
    10.                         分组1 子项2
    11.                     
    12.                 
    13.                 
    14.                     
    15.                         分组1 子项3
    16.                     
    17.                 
    18.             
    19.             
    20.                 
    21.                     
    22.                         分组2 子项1
    23.                     
    24.                 
    25.                 
    26.                     
    27.                         分组2 子项2
    28.                     
    29.                 
    30.                 
    31.                     
    32.                         分组2 子项3
    33.                     
    34.                 
    35.             
    36.         
    37.     

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

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

     hml:

     
     
     
     
    1.      
    2.          
    3.              
    4.                  
    5.                      
    6.                          
    7.                          {{ $item.name }}
    8.                      
    9.                      
    10.                          
    11.                              
    12.                              {{ value.name }}
    13.                          
    14.                      
    15.                  
    16.              
    17.          
    18.      
    19.      

    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.    }

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

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

    https://harmonyos./#zz

    分享题目:从微信小程序到鸿蒙JS开发【04】-list组件
    标题链接:http://www.hantingmc.com/qtweb/news6/358706.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

    猜你还喜欢下面的内容

    服务器托管知识

    同城分类信息