HarmonyOS 点击list的item,修改当前item的属性状态值List根据属性值变化刷新UI?

如题:HarmonyOS 点击list的item,修改当前item的属性状态值List根据属性值变化刷新UI?

阅读 561
1 个回答

参考如下demo:

export class ListModel {
  title: string;
  content: string;
  isComplete: boolean;

  constructor(title: string, content: string, isComplete: boolean) {
    this.title = title;
    this.content = content;
    this.isComplete = isComplete;
  }
}
@Entry
@Component
struct SyncPage {
  @State ListData: Array<ListModel> = [
    new ListModel('标题1', '内容1', true),
    new ListModel('标题2', '内容2', false),
    new ListModel('标题3', '内容3', false),
  ];

  build() {
    Column() {
      List() {
        ForEach(this.ListData, (item: ListModel, index: number) => {
          ListItem() {
            Row() {
              Row() {
                Text(item.title)
                Text(item.content).fontColor('#999999').fontSize(13)
              }

              Image(item.isComplete ? $r('app.media.ic_personal_focus') : $r('app.media.ic_personal_normal'))
                .width(20)
                .height(20)
                .margin(10)
            }
            .justifyContent(FlexAlign.SpaceBetween)
            .width('100%')
            .alignItems(VerticalAlign.Center)
            .height(80)
            .onClick(() => {
              let status = !item.isComplete
              this.ListData.map((el, itemIndex) => {
                if (itemIndex !== index) {
                  el.isComplete = false
                }
              })
              this.ListData.splice(index, 1, new ListModel(item.title, item.content, status))
            })
          }
        })
      }

      Button('确认', { type: ButtonType.Normal })
        .onClick(() => {
          console.log('打印listData选择的数据')
        })
    }

  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进