#鸿蒙通关秘籍#如何在鸿蒙中实现动态网格布局刷新

HarmonyOS
22h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
紫电青霜CPM

在鸿蒙开发中,需要实现动态网格布局并支持刷新功能。通过以下代码实现下拉刷新效果并动态更新网格布局中的内容:

html <!-- index.hml --> <div class="container"> <refresh refreshing="`fresh`" onrefresh="refresh"> <grid-container id="mygrid" gutter="20" style="margin: 10px;"> <grid-row style="height:200px;width: 100%;background-color: #e7e7e2;margin-top: 50px; padding: 0px 20px;border-radius: 15px;" for="item in list"> <grid-col span="0" style="width: 40%;"> <div style="align-items: center;justify-content: center"> <image src="`item`.`src`" style="object-fit: contain;border-radius: 30px;"></image> </div> </grid-col> <grid-col span="0" style="width: 60%;"> <div style="align-items: center;justify-content: center;width: 100%;height: 100%;text-align: center;"> <text>image`item`.`id`</text> </div> </grid-col> </grid-row> </grid-container> </refresh> </div>

css /* xxx.css */ .container{ flex-direction: column; background-color: #F1F3F5; width: 100%; height: 100%; } text{ color: #0a0aef; font-size: 60px; }

javascript // index.js import promptAction from '@ohos.promptAction'; export default { data:{ list:[ {src:'common/images/1.png',id:'1'}, {src:'common/images/2.png',id:'2'}, {src:'common/images/3.png',id:'3'} ], fresh:false }, refresh(e) { promptAction.showToast({ message: 'refreshing' }) var that = this; that.fresh = e.refreshing; setTimeout(function () { that.fresh = false; that.list.unshift({src: 'common/images/4.png',id:'4'}); promptAction.showToast({ message: 'succeed' }) }, 2000) } }

触发下拉刷新事件时,通过异步操作更新图像列表list,并及时更新界面显示新的布局。

分享
微博
QQ
微信
回复
19h前
相关问题
【急】鸿蒙UI界面网格布局怎么设置?
16488浏览 • 4回复 待解决