HarmonyOS 下面demo为什么点击不刷新?

@Observed 
class HSItemData{ 
  choose:boolean = false 
} 
@Observed 
class HSTestDataModel{ 
  itemArray:Array<HSItemData> = new Array() 
} 
 
@Component 
struct HSCell{ 
  @ObjectLink itemData:HSItemData 
  tapEvent?:(itemData:HSItemData)=>void 
  build() { 
    Row(){ 
      if (this.itemData.choose){ 
        Text("选中") 
      } 
      else { 
        Text("未选中") 
      } 
    } 
    .onClick(()=>{ 
      if (this.tapEvent){ 
        this.tapEvent(this.itemData) 
      } 
    }) 
    .height(68) 
  } 
} 
@Entry({ routeName: 'HSTestPage' }) 
@Component 
export struct HSTestPage { 
  listeners: DataChangeListener[] = [] 
  @State dataModel :HSTestDataModel  = new HSTestDataModel() 
  aboutToAppear(): void { 
    for (let i = 0; i < 10;i ++){ 
      this.dataModel.itemArray.push(new HSItemData()) 
    } 
  } 
  build() { 
    Column(){ 
      List(){ 
        LazyForEach(this,(item: HSItemData, index)=>{ 
          HSCell({itemData:item,tapEvent:(item)=>{ 
            item.choose = !item.choose 
          }}) 
        }) 
      } 
    } 
    .height("100%") 
  } 
  totalCount() { 
    return this.dataModel.itemArray.length 
  } 
  getData(index: number) { 
    return this.dataModel.itemArray[index] 
  } 
  registerDataChangeListener(listener: DataChangeListener) { 
    if (this.listeners.indexOf(listener) < 0) { 
      this.listeners.push(listener); 
    } 
  } 
  unregisterDataChangeListener(listener: DataChangeListener) { 
    const pos = this.listeners.indexOf(listener); 
    if (pos >= 0) { 
      this.listeners.splice(pos, 1); 
    } 
  } 
  //刷新页面 
  reloadTableView(): void { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded(); 
    }) 
  } 
}

上述demo为什么点击不刷新?

HarmonyOS
2024-09-03 10:32:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

您可参考以下代码:

@Observed 
class HSItemData{ 
  choose:boolean = false 
} 
@Observed 
class HSTestDataModel{ 
  itemArray:Array<HSItemData> = new Array() 
} 
 
@Component 
struct HSCell{ 
  @ObjectLink itemData:HSItemData 
  tapEvent?:(itemData:HSItemData)=>void 
  build() { 
    Row(){ 
      if (this.itemData.choose){ 
        Text("选中") 
      } 
      else { 
        Text("未选中") 
      } 
    } 
    // .onClick(()=>{ 
    //   if (this.tapEvent){ 
    //     this.tapEvent(this.itemData) 
    //   } 
    // }) 
    .height(68) 
  } 
} 
 
@Entry({ routeName: 'HSTestPage' }) 
@Component 
export struct HSTestPage { 
  listeners: DataChangeListener[] = [] 
  @State dataModel :HSTestDataModel = new HSTestDataModel() 
  aboutToAppear(): void { 
    for (let i = 0; i < 10;i ++){ 
      this.dataModel.itemArray.push(new HSItemData()) 
    } 
  } 
  build() { 
    Column(){ 
      List(){ 
        LazyForEach(this,(item: HSItemData, index)=>{ 
          HSCell({itemData:item,tapEvent:(item)=>{ 
            // item.choose = !item.choose 
            // console.log(item.choose.toString()) 
          }}).onClick(()=> { 
            item.choose = !item.choose 
          }) 
        }, (item: string) => item.toString()) 
      } 
    }.height("100%") 
  } 
  totalCount() { 
    return this.dataModel.itemArray.length 
  } 
  getData(index: number) { 
    return this.dataModel.itemArray[index] 
  } 
  registerDataChangeListener(listener: DataChangeListener) { 
    if (this.listeners.indexOf(listener) < 0) { 
      this.listeners.push(listener); 
    } 
  } 
  unregisterDataChangeListener(listener: DataChangeListener) { 
    const pos = this.listeners.indexOf(listener); 
    if (pos >= 0) { 
      this.listeners.splice(pos, 1); 
    } 
  } 
  //刷新页面 
  reloadTableView(): void { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded(); 
    }) 
  } 
}
分享
微博
QQ
微信
回复
2024-09-03 15:41:41
相关问题
Left、Top为什么生效?
758浏览 • 1回复 待解决
HarmonyOS使用@ObjectLink 数据刷新
713浏览 • 2回复 待解决
HarmonyOS 装饰器刷新问题
155浏览 • 1回复 待解决
IF条件变化后UI刷新
726浏览 • 1回复 待解决
HarmonyOS 状态变量刷新问题
443浏览 • 1回复 待解决
状态装饰器 ui刷新的问题
2441浏览 • 1回复 待解决