HarmonyOS 父组件内有个自定义的类列表,用Grid布局到界面上,GridItem是一个自定义的@Component,如何在父组件控制刷新自定义的@Component

父组件内有个自定义的类列表,用Grid布局到界面上,GridItem是一个自定义的@Component,如何在父组件控制刷新自定义的@Component

大致代码如下

数据类

export default class PrivacyItem1 {
  selected: boolean = false;
  path: string = '';
}

自定义子组件

@Component
export struct PrivacyItemView1 {
  private itemWidth = 120;
  @Link editMode: boolean;
  @State data: PrivacyItem1 = {selected:false, path: ''};
  private onSelectChange: ((pi: PrivacyItem1, selected: boolean) => void) | undefined;
  private onItemClick:((pi:PrivacyItem1) => void) | undefined;
  @State selected:boolean = false;
  build() {
    RelativeContainer() {
      Text(this.data.path).alignRules({
        center:{anchor:'__container__',align:VerticalAlign.Center},
        middle:{anchor:'__container__',align:HorizontalAlign.Center},
      })

      if (this.editMode && this.selected) {
        Row(){
          Image($r('app.media.select')).width(40)
        }.width(this.itemWidth).height(this.itemWidth).backgroundColor(0x80000000).justifyContent(FlexAlign.Center)
      }
    }.width(this.itemWidth)
    .height(this.itemWidth)
    .border({
      width: 1,
      color: $r('app.color.lightGrey'),
      radius: 6
    }).onClick((event) => {
      if (this.editMode) {
        this.selected = !this.selected;
        if (this.onSelectChange!=undefined) {
          this.onSelectChange(this.data, this.selected);
        }
      }else{
        if (this.onItemClick != undefined) {
          this.onItemClick(this.data);
        }
      }
    })
  }
}

父组件

@Entry
@Component
struct HideFileManager {
  @State privacyList: PrivacyItem1[] =
    [{ selected: false, path: '1' }, { selected: false, path: '2' }, { selected: false, path: '3' },
      { selected: false, path: '4' }];
  @State editMode:boolean = true;
  build() {
    Column() {
      Button('select all').onClick((event) => {
        // 这里要如何控制才能 改变子组件列表?
      })
      Grid() {
        ForEach(this.privacyList, (item: PrivacyItem1, i) => {
          GridItem() {
            PrivacyItemView1({
              data: item,
              editMode: this.editMode,
              onSelectChange: (pi: PrivacyItem1, selected: boolean) => {
                pi.selected = selected;
              },
              onItemClick: (pi: PrivacyItem1) => {

              }
            })
          }
        })
      }
    }
  }
}

应该如何在 select all 这个按钮的点击事件中,刷新所有的GridItem,让它们内部的selected变量改为true?

HarmonyOS
2024-12-26 07:14:48
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect
分享
微博
QQ
微信
回复
2024-12-26 10:36:01
相关问题
如何自定义Component 属性
15280浏览 • 3回复 待解决
HarmonyOS 实现一个自定义分类列表
583浏览 • 1回复 待解决
如何在自定义函数中创建一个UI组件
2042浏览 • 1回复 待解决
HarmonyOS 如何自定义布局组件
107浏览 • 1回复 待解决
Grid组件scrollBar是否支持自定义
2519浏览 • 1回复 待解决