ListItemGroup能跟LazyForEach搭配使用吗

ListItemGroup能跟LazyForEach搭配使用吗?

HarmonyOS
2024-06-05 20:48:34
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
hmogy

请参考如下示例代码:

// Index.ets 
import hilog from '@ohos.hilog'; 
 
const TAG: string = 'testTag' 
 
class BasicDataSource implements IDataSource { 
  private listeners: DataChangeListener[] = []; 
  private originDataArray: string[] = []; 
 
  public totalCount(): number { 
    return 0; 
  } 
 
  public getData(index: number): string { 
    return this.originDataArray[index]; 
  } 
 
  registerDataChangeListener(listener: DataChangeListener): void { 
    if (this.listeners.indexOf(listener) < 0) { 
      console.info('add listener'); 
      this.listeners.push(listener); 
    } 
  } 
 
  unregisterDataChangeListener(listener: DataChangeListener): void { 
    const pos = this.listeners.indexOf(listener); 
    if (pos >= 0) { 
      console.info('remove listener'); 
      this.listeners.splice(pos, 1); 
    } 
  } 
 
  notifyDataReload(): void { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded(); 
    }) 
  } 
 
  notifyDataAdd(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataAdd(index); 
    }) 
  } 
 
  notifyDataChange(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataChange(index); 
    }) 
  } 
 
  notifyDataDelete(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataDelete(index); 
    }) 
  } 
} 
 
class MyDataSource extends BasicDataSource { 
  private dataArray: string[] = []; 
 
  public totalCount(): number { 
    return this.dataArray.length; 
  } 
 
  public getData(index: number): string { 
    return this.dataArray[index]; 
  } 
 
  public addData(index: number, data: string): void { 
    this.dataArray.splice(index, 0, data); 
    this.notifyDataAdd(index); 
  } 
 
  public pushData(data: string): void { 
    this.dataArray.push(data); 
    this.notifyDataAdd(this.dataArray.length - 1); 
  } 
 
  public deleteData(index: number): void { 
    this.dataArray.splice(index, 1); 
    this.notifyDataDelete(index); 
  } 
 
  public changeData(index: number, data: string): void { 
    this.dataArray.splice(index, 1, data); 
    this.notifyDataChange(index); 
  } 
}
@Entry 
@Component 
struct WaterflowDemo { 
  private data: MyDataSource = new MyDataSource(); 
  private data1: MyDataSource = new MyDataSource(); 
 
  aboutToAppear() { 
    for (let i = 0; i <= 20; i++) { 
      this.data.pushData(`Hello ${i}`) 
    } 
    for (let i = 0; i <= 20; i++) { 
      this.data1.pushData(`Hello ${i}`) 
    } 
  } 
 
  build() { 
    Column() { 
      List({ space: 10 }) { 
        ListItemGroup() { 
          LazyForEach(this.data, (item: string, index: number) => { 
            ListItem() { 
              Row() { 
                Text(item).fontSize(50) 
                  .onAppear(() => { 
                    console.info("appear:" + item) 
                  }) 
              }.margin({ left: 10, right: 10 }) 
            } 
            .onAppear(() => { 
              hilog.info(0x00000, TAG, '新数据111'); 
            }) 
          }) 
        } 
 
        ListItemGroup() { 
          LazyForEach(this.data1, (item: string, index: number) => { 
            ListItem() { 
              Row() { 
                Text(item).fontSize(50) 
                  .onAppear(() => { 
                    console.info("appear:" + item) 
                  }) 
              }.margin({ left: 10, right: 10 }) 
            } 
            .onAppear(() => { 
              hilog.info(0x00000, TAG, '新数据222'); 
            }) 
          }) 
        } 
      } 
      .width('90%') 
      .sticky(StickyStyle.Header | StickyStyle.Footer) 
      .scrollBar(BarState.Off) 
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 }) 
  } 
}
分享
微博
QQ
微信
回复
2024-06-06 21:17:01
相关问题
ListItemGrouplazyforeach如何结合使用
637浏览 • 1回复 待解决
ListItemGroupLazyforeach结合场景
1001浏览 • 1回复 待解决
如何搭配使用
2123浏览 • 1回复 待解决
list 如何使用 lazyforeach
280浏览 • 1回复 待解决
HarmonyOS swiper + LazyForEach使用问题
309浏览 • 1回复 待解决
如何搭配使用
494浏览 • 1回复 待解决
LazyForEach使用限制有哪些?
595浏览 • 1回复 待解决
使用List lazyForeach时,reuseId未生效
323浏览 • 1回复 待解决
使用LazyForEach懒加载列表相关问题
873浏览 • 1回复 待解决
HarmonyOS LazyForEach
336浏览 • 1回复 待解决
鸿蒙要王者荣耀
6401浏览 • 1回复 待解决
arkTs和arkjs互相调用
2194浏览 • 1回复 待解决
修改Button的默认样式
362浏览 • 1回复 待解决