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如何结合使用
202浏览 • 1回复 待解决
ListItemGroupLazyforeach结合场景
416浏览 • 1回复 待解决
如何搭配使用
795浏览 • 1回复 待解决
如何搭配使用
36浏览 • 1回复 待解决
使用LazyForEach懒加载列表相关问题
377浏览 • 1回复 待解决
TaskPool如何主线程进行通信
25浏览 • 1回复 待解决
ArkTS runtime之前的maple有什么区别
732浏览 • 1回复 待解决
鸿蒙要王者荣耀
5109浏览 • 1回复 待解决
首页LazyForEach predict耗时久分析
381浏览 • 1回复 待解决
list的addremove item的入场、出场动画
484浏览 • 1回复 待解决
arkTs和arkjs互相调用
1049浏览 • 1回复 待解决
mate 40 pro升级鸿蒙
8857浏览 • 1回复 待解决
XML CheckBox 图标间隔设置
3301浏览 • 1回复 待解决
鸿蒙应用访问webapi接口
3739浏览 • 1回复 待解决
LazyForEach懒加载的原理是什么
924浏览 • 1回复 待解决