HarmonyOS 列表多选页面无法刷新

HarmonyOS
2024-12-25 14:31:58
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

参考示例如下:

import { LinkedList } from '@kit.ArkTS';

@Entry
@Component
struct Index {
  @State arrayListMoodThing: dataEnty[][] =
    [[new dataEnty(0, '电影1', false), new dataEnty(1, '电影1', false), new dataEnty(2, '电影1', false),
      new dataEnty(3, '电影1', false), new dataEnty(4, '电影1', false), new dataEnty(5, '电影1', false)],
      [new dataEnty(6, '电影1', false), new dataEnty(7, '电影1', false), new dataEnty(8, '电影1', false),
        new dataEnty(9, '电影1', false), new dataEnty(10, '电影1', false), new dataEnty(11, '电影1', false)]];
  @State arrSelectedIds: Array<dataEnty> = new Array()

  build() {
    Column() {
      Swiper() {
        ForEach(this.arrayListMoodThing, (datas: dataEnty[], index: number) => {
          Grid() {
            ForEach(datas, (data: dataEnty, i: number) => {
              GridItem() {
                ViewA({
                  label: data.txt, data: data, onClickItem: (data) => {
                    this.arrSelectedIds.push(data)
                    console.log('this.arrSelectedIds----', JSON.stringify(this.arrSelectedIds))
                    if (this.arrSelectedIds.length > 3) {
                      let needChangeItem = this.arrSelectedIds[0]
                      this.arrSelectedIds.splice(0, 1)
                      for (let index = 0; index < this.arrayListMoodThing.length; index++) {
                        const element = this.arrayListMoodThing[index];
                        for (let index = 0; index < element.length; index++) {
                          const item = element[index];
                          if (item.id == needChangeItem.id) {
                            item.selected = false
                            item.txt = '电影1'
                          }
                        }
                      }
                    }

                  }
                })
              }
              .width(80)
              .height(80)
            });
          }
          .columnsTemplate('1fr 1fr 1fr')
          .rowsTemplate('1fr 1fr 1fr')
          .width('100%')
          .height('100%')
          .rowsGap(10)
          .columnsGap(10)
          .margin({ bottom: 20, right: 20, left: 20 });
        });
      }
      .width('100%')
      .height(300)
      .width('100%')
      .height('100%')
      .backgroundColor('#ffffff')
      .padding({ top: 10 })
    }
  }
}

@Observed
class dataEnty {
  txt: string = ""
  selected: boolean = false
  id: number = 0

  constructor(id: number, txt: string, sel: boolean) {
    this.id = id
    this.txt = txt
    this.selected = sel

  }
}

@Component
struct ViewA {
  // 子组件ViewA的@ObjectLink的类型是ClassA
  @ObjectLink data: dataEnty;
  label: string = 'ViewA1';
  /*选中的id*/

  onClickItem?: (item: dataEnty) => void

  build() {
    Row() {
      Button(` ${this.data ? this.data.txt : "undefined"}`)
        .margin(10)
        .onClick(() => {
          this.data.selected == true;
          this.data.txt = 'sss'
          if (this.onClickItem != undefined) {
            this.onClickItem(this.data)
          }

        })
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-25 16:43:54
相关问题
HarmonyOS webview加载页面无法显示
1017浏览 • 1回复 待解决
HarmonyOS 列表中checkbox多选
149浏览 • 1回复 待解决
HarmonyOS 分屏模式下页面无法滑动
77浏览 • 1回复 待解决
HarmonyOS hap跳hsp页面无法跳转
326浏览 • 1回复 待解决
HarmonyOS scroll嵌套list页面无法滑动
347浏览 • 1回复 待解决
HarmonyOS Har静态包内部页面无法跳转
762浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
868浏览 • 1回复 待解决
鸿蒙webview 上面无法放其它组件。
5971浏览 • 3回复 待解决
HarmonyOS 通知列表刷新事件
297浏览 • 1回复 待解决
HarmonyOS ForEach列表刷新问题
354浏览 • 1回复 待解决