HarmonyOS 鸿蒙Next 循环渲染不出内容
HarmonyOS 鸿蒙Next 循环渲染不出内容 大佬们求助一下我这个循环渲染为啥不能把列表里的内容显示?
export default class ItemData{
title_1:Resource;//标题
title_2:Resource;//进度
title_3:Resource;//日期
//constructor这个是关键字,类似于构造函数
constructor(title_1:Resource,title_2?:Resource,title_3?:Resource)
{
this.title_1=title_1;
this.title_2=title_2;
this.title_3=title_3;
}
}
import ItemData from '../pages/ListItem_class'
export class MainViewModel{
getSettingListData():Array<ItemData> {
let settingListData:ItemData[]=[
new ItemData($r("app.string.Objective_1"),$r("app.string.percent_100"),$r("app.string.Time_1")),
new ItemData($r("app.string.Objective_2"),$r("app.string.percent_75"),$r("app.string.Time_2")),
new ItemData($r("app.string.Objective_3"),$r("app.string.percent_50"),$r("app.string.Time_3")),
new ItemData($r("app.string.Objective_4"),$r("app.string.percent_25"),$r("app.string.Time_4"))
]
return settingListData;
}
}
export default new MainViewModel();
import ItemData from '../pages/ListItem_class'
import MainViewModel from '../pages/ListItem_data'
@Entry
@Component
struct Index {
@Builder settingCell(item:ItemData) {
Column() {
Row() {//
Text(item.title_1)
.fontSize(10)
.fontWeight(FontWeight.Bold)
.height(50)
.width(50)
Column() {
Text(item.title_2)
.fontSize(10)
Text(item.title_3)
.fontSize(5)
}//
}
//.justifyContent(FlexAlign.SpaceBetween)//对齐
.borderRadius(10)//圆角
.justifyContent(FlexAlign.SpaceBetween)//设置对齐方式
.width('100%')
//进度条
}
.height(100)
.backgroundColor("yellow")
}
build() {
Column() {
Row(){
Text('子目标')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text('编辑')
.fontSize(15)
.fontColor('blue')
}
.width("95%")
.justifyContent(FlexAlign.SpaceBetween)
.backgroundColor("#66FFCC")
Column(){
List({space:10}){
ForEach(
MainViewModel.getSettingListData(),//循环渲染
(item:ItemData)=>{
ListItem(){
this.settingCell(item)
}
},item=>JSON.stringify(item)
)
}
.width('100%')
.height('50%')
.divider({strokeWidth:20,color:'green'})//分割线
.borderRadius(30)//圆角
}
.backgroundColor("#CC33CC")
//多个目标项
}
.width('100%')
}
}
结果是这样的,还有这个圆角也没效果
更多关于HarmonyOS 鸿蒙Next 循环渲染不出内容的实战教程也可以访问 https://www.itying.com/category-93-b0.html
圆角有效果啊,只不过有圆角没有背景色你看不出来而已
更多关于HarmonyOS 鸿蒙Next 循环渲染不出内容的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
还真是,我把背景色加在Column下了。谢谢,
,item => JSON.stringify(item)
这个不影响吧?我删了还是不行,
在HarmonyOS(鸿蒙)Next中,循环渲染不出内容可能是由于以下原因导致的:
-
数据源问题:检查数据源是否为空或未正确绑定。确保数据源中有数据,并且数据格式符合预期。
-
ForEach组件使用不当:确保使用了正确的ForEach组件来遍历数据源。ForEach组件应正确绑定数据源,并在每次循环中生成相应的子组件。
-
条件渲染问题:如果使用了条件渲染(如if/else),确保条件判断逻辑正确,避免因条件不满足而导致内容无法渲染。
-
组件嵌套问题:检查组件嵌套是否正确,确保循环渲染的组件在父组件中正确显示。
-
样式或布局问题:检查样式和布局设置,确保渲染的内容不会被隐藏或覆盖。
-
状态管理问题:如果使用了状态管理,确保状态更新正确触发渲染。
-
开发工具问题:确保开发工具(如DevEco Studio)版本与HarmonyOS版本兼容,避免因工具问题导致渲染异常。
通过以上步骤排查问题,可以有效解决HarmonyOS Next中循环渲染不出内容的情况。