当前位置:主页 > 查看内容

鸿蒙的横向滚动菜单和分组Group列表菜单和fetch请求加载聚合数据

发布时间:2021-05-20 00:00| 位朋友查看

简介:想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com/#zz 本文的项目是通过远程调用聚合数据制作一个新闻页面.首先要明确以下几点: 1.页面加载的时候 用户点击加载浏览什么服务器就加载什么,若一下全部加载出……

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

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

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

本文的项目是通过远程调用聚合数据制作一个新闻页面.首先要明确以下几点:

1.页面加载的时候 用户点击加载浏览什么服务器就加载什么,若一下全部加载出来不仅对页面布局有负担,而且对服务器的负载也很大,造成服务器资源浪费.

2.思路过程(以制作首页为例): onInit首先加载拿到首页数据;用户点击那个菜单加载那个菜单的数据.

项目过程如下:

1.导入鸿蒙的网络请求模块fetch (上篇文章也讲到) 这里需要重点注意的是:一定要到config.json中去看有没有配置二级域名 不然看不到数据.例如我这里是v.juhe.cn 下图:


2.因为鸿蒙没有可视化窗口 ,因此我们可以制作一个调试可视化窗口.调试完成后 再将其去掉 . 这个窗口的目的仅是让自己通过可视化窗口清楚明了的看到是否成功拿到数据,便于我们更好的跟踪和调试.(点击事件changemenu跳转到js中 将当前点击的菜单项赋值给currentIndex就做制作一个调试窗口)具体代码及标注以下会详细给出. 展示想过如下图:

 

模拟器中的字样来自于聚合数据给定的样式:

js业务逻辑层:

  1. import fetch from '@system.fetch'
  2. export default { 
  3.     data: { 
  4.         title:''
  5.         currentIndex:0, 
  6.         type:""
  7.         bookdatas:[],  //申明一个数组 
  8.        menus:["首页","新闻","影视","音乐","天气","生活","体育","电竞","娱乐","美食"
  9.     }, 
  10.     onInit() { 
  11.          
  12.  
  13.         fetch.fetch({ 
  14.             url:"http://v.juhe.cn/toutiao/index?type=top&key=401162c3e198047abc4d73d6f95aabbe"
  15.             //v.juhe.cn一定要到config.json中去看有没有配置二级域名  
  16.             responseType:"json"
  17.             success:(resp)=>{ 
  18.                 let datas=JSON.parse(resp.data);   //转换成json数据格式   
  19.                 this.title=datas.reason; 
  20.                 this.bookdatas=datas.result.data; 
  21.                
  22.  
  23.             } 
  24.  
  25.         } 
  26.  
  27.     }, 
  28.     changemenu(event){ 
  29.            let clickindex=event.index
  30.            this.currentIndex=clickindex ; //当前点击的菜单项赋值给currentIndex 
  31.            this.type=typeof clickindex; 
  32.         if(clickindex==0) 
  33.            { 
  34.  
  35.            } 
  36.          else if(clickindex==1) 
  37.           { 
  38.  
  39.           } 
  40.     } 

页面渲染:

  1. <div class="container"
  2.     <!--鸿蒙没有横向和垂直的滚顶视图组件--> 
  3.     <tabs class="tabs" index="{{currentIndex}}" vertical="false" onchange="changemenu"
  4.         <tab-bar class="tab-bar" mode="scrollable"
  5.             <block for="{{menus}}"
  6.                 <text>{{$item}}</text> 
  7.             </block> 
  8.         </tab-bar> 
  9.         <tab-content class="tab-content" scrollable="true"
  10.             <div class="oneview"
  11.                 <list class="listview">              //运用列表展示数据 
  12.                     <block for="{{bookdatas}}"
  13.                         <list-item class="list-item"
  14.                             <text>{{$item.title}}</text> 
  15.                         </list-item> 
  16.                     </block> 
  17.                 </list> 
  18.             </div> 
  19.             <div class="twoview"
  20.                 <text>two</text> 
  21.             </div> 
  22.         </tab-content> 
  23.     </tabs> 
  24.      <div class="info"
  25.          <text class="txtview">{{currentIndex}} </text>  //返回当前标 
  26.          <text class="txtview">{{type}} </text>   //返回当前下表的数值类型 
  27.          <text class="txtview">{{title}} </text>   //是否成功获取数据 
  28.      </div> 
  29. </div> 

css属性设置:

  1. .container{ 
  2.     width: 100%; 
  3.     height: 1200px; 
  4.     background-color: palegreen ; 
  5.     display: flex; 
  6.     flex-direction: column
  7. .tabs{ 
  8.     width: 100%; 
  9.  
  10. .tab-content{ 
  11.     width: 100%; 
  12.     height: 80%; 
  13.     background-color: green; 
  14. .oneview{ 
  15.     width: 100%; 
  16.     height: 100%; 
  17.     background-color: white; 
  18. .twoview{ 
  19.     width: 100%; 
  20.     height: 100%; 
  21.     background-color: skyblue; 
  22. .info{ 
  23.     width: 100%; 
  24.     height: 20%; 
  25.     border:1px solid red; 
  26. .txtview{ 
  27.     font-size: 50px; 
  28.     color: red; 
  29. .listview{ 
  30.     width: 100%; 
  31.     height: 100%; 
  32. .list-item{ 
  33.     width: 100%; 
  34.     height: 20%; 
  35.     border: 1px solid red; 

最后再强调一点 在调用聚合数据的时候代码的格式一定要和聚合给定的格式相同,通俗的来讲就是例如:我这里想获取的是是result中的data中的title的数据(见下图) 因此我们同样要将获取的数组放到申明的bookdatas数组中

 

最终效果图如下:


这样项目的大体框架就出来了 其他页面也是如此.后续会继续优化页面的布局.

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任。

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

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

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


本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐