HarmonyOS LIst组件UI不刷新

使用list组件ForEach渲染,数据改变ui不变问题。

HarmonyOS LIst组件UI不刷新 -鸿蒙开发者社区

HarmonyOS
2025-01-09 16:46:46
436浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

可使用@Observed装饰器和@ObjectLink装饰器来监听数据的变化,支持文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#对象数组

参考示例如下:

@Observed
export class ControlList extends Array<Controls> {
  constructor() {
    super();
  }
}

@Observed
export class Controls {
  uuid: string
  control_type: string
  control_weights: string
  control_name: string
  control_status: string
  control_location: string
  control_menu_levels: string
  control_position: string
  control_h_name: string
  control_value: string
  isSelect: boolean

  constructor(param: Controls) {
    this.uuid = param.uuid
    this.control_type = param.control_type
    this.control_weights = param.control_weights
    this.control_name = param.control_name
    this.control_status = param.control_status
    this.control_location = param.control_location
    this.control_menu_levels = param.control_menu_levels
    this.control_position = param.control_position
    this.control_h_name = param.control_h_name
    this.control_value = param.control_value
    this.isSelect = param.isSelect
  }
}

@Component
export struct TestPage {
  @ObjectLink dataSource: ControlList

  build() {
    Column() {
      List({ space: 10 }) {
        ForEach(this.dataSource, (item: Controls, index) => {
          ListItem() {
            ControlItem({ item: item, dataSource: this.dataSource, })
          }
        })
      }.margin({ top: 10, bottom: 10 })
    }
  }
}

@Component
struct ControlItem {
  @ObjectLink item: Controls
  @Link dataSource: ControlList

  build() {
    Row() {
      if (this.item.isSelect) {
        Row() {
          Text(this.item.control_h_name).fontSize(15).fontColor('#B11F1A')
          Image($r('app.media.startIcon')).width(20).height(20)
        }
        .width('100%')
        .borderRadius(18)
        .borderWidth(1)
        .borderColor('#FFAE2921')
        .padding(5)
        .justifyContent(FlexAlign.SpaceBetween)
      } else {
        Row() {
          Text(this.item.control_h_name).fontSize(15).fontColor('#525252')
        }
        .width('100%')
        .borderRadius(18)
        .borderWidth(1)
        .borderColor('#FFE3E4E4')
        .padding(5)
      }
    }.onClick(() => {
      this.item.isSelect = !this.item.isSelect
      this.dataSource.splice(0, 1);
      console.log('isSelect', JSON.stringify(this.item));
    })
  }
}

@Entry
@Component
struct Page {
  @State dataSource: ControlList = new ControlList()

  aboutToAppear(): void {
    let json =
      `{"uuid":"13","control_type":"1","control_weights":"0.400000","control_name":"起投金额","control_value":"buyerSmallestAmountQuery","control_h_name":"10万(含)-20万","control_h_value":"7;","control_status":"1","control_location":"transferList","control_menu_levels":"1","control_position":"second","isSelect":true}`
    let item: Controls = JSON.parse(json)
    for (let i = 1; i < 6; i++) {
      this.dataSource.push(new Controls(item));
    }
  }

  build() {
    Column() {
      TestPage({ dataSource: this.dataSource })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.

参考链接:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/properly-use-state-management-to-develope.md#%E4%BD%BF%E7%94%A8observed%E8%A3%85%E9%A5%B0%E6%88%96%E8%A2%AB%E5%A3%B0%E6%98%8E%E4%B8%BA%E7%8A%B6%E6%80%81%E5%8F%98%E9%87%8F%E7%9A%84%E7%B1%BB%E5%AF%B9%E8%B1%A1%E7%BB%91%E5%AE%9A%E7%BB%84%E4%BB%B6

分享
微博
QQ
微信
回复
2025-01-09 18:22:53


相关问题
HarmonyOS UI刷新
690浏览 • 1回复 待解决
HarmonyOS UI刷新问题
665浏览 • 1回复 待解决
HarmonyOS 更新数据后UI刷新
824浏览 • 1回复 待解决
IF条件变化后UI刷新
1254浏览 • 1回复 待解决
HarmonyOS @builder方法的ui刷新
1002浏览 • 1回复 待解决
状态装饰器 ui刷新的问题
2886浏览 • 1回复 待解决
HarmonyOS 懒加载列表更改属性UI刷新
595浏览 • 1回复 待解决
修改ForEach使用的数据对象,UI刷新
2770浏览 • 1回复 待解决
HarmonyOS List组件动态刷新数据问题
1721浏览 • 1回复 待解决
HarmonyOS List组件指定item刷新实现方案
758浏览 • 1回复 待解决
HarmonyOSList组件是否支持局部刷新
1121浏览 • 1回复 待解决
HarmonyOS List怎么刷新数据?
680浏览 • 1回复 待解决