ListItemGroup 和Lazyforeach结合场景

ListItemGroup 和Lazyforeach如何结合

HarmonyOS
2024-05-26 12:29:00
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
sunshine_2000

使用的核心API

Lazyforeach

ListItemGroup

核心代码解释

官网有ForEach与ListItemGroup结合的示例,但是现在的开发场景是分组未知请每一个组的数据也未知,并且也需要懒加载分页加载数据,在这种情况下就需要使用Lazyforeach与ListItemGroup相结合。

需要自己按照IDataSource接口来自己维护一个MyDataSource1和MyDataSource2来管理数据,在MyDataSource2中需要嵌套一个MyDataSource1以此来达到效果

同事使用Math对象随机生成数据来模拟上述场景。

实现代码如下所示:

@Entry 
@Component 
struct ListItemGroupExample { 
  private list: MyDataSource2 = new MyDataSource2() 
 
  @Builder 
  itemHead(text: string) { 
    Text(text) 
      .fontSize(20) 
      .backgroundColor(0xAABBCC) 
      .width("100%") 
      .padding(10) 
  } 
 
  @Builder 
  itemFoot(num: number) { 
    Text('共' + num + "节课") 
      .fontSize(16) 
      .backgroundColor(0xAABBCC) 
      .width("100%") 
      .padding(5) 
  } 
 
  aboutToAppear() { 
    for (let date = 1; date < ~~(Math.random() * 30) + 3; date++) { 
      let dayData = new dateListItem(date + "") 
      for (let index = 1; index < ~~(Math.random() * 100) + 30; index++) { 
        dayData.orderList.pushData(`hello${index}`) 
      } 
      this.list.pushData(dayData) 
    } 
  } 
 
  build() { 
    Column() { 
      List({ space: 20 }) { 
        LazyForEach(this.list, (item: dateListItem, index) => { 
          ListItemGroup({ header: this.itemHead(item.date + ""), footer: this.itemFoot(item.orderList.totalCount()) }) { 
            LazyForEach(item.orderList, (order: string, index2) => { 
              ListItem() { 
                Text(order) 
                  .onAppear(() => { 
                    console.info("appear:" + order, index2 + "") 
                  }) 
                  .width("100%") 
                  .height(60) 
                  .fontSize(20) 
                  .textAlign(TextAlign.Center) 
                  .backgroundColor(0xFFFFFF) 
              } 
            }, (item: string) => item) 
          } 
          .onAppear(() => { 
            console.info("appear:" + item, index + "") 
          }) 
          .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线 
        }) 
      } 
      .cachedCount(1) 
      .width('90%') 
      .sticky(StickyStyle.Header | StickyStyle.Footer) 
      .scrollBar(BarState.Off) 
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 }) 
  } 
} 
 
 
// Basic implementation of IDataSource to handle data listener 
class BasicDataSource implements IDataSource { 
  private listeners: DataChangeListener[] = []; 
  private originDataArray: string[] = []; 
 
  public totalCount(): number { 
    return 0; 
  } 
 
  public getData(index: number): string | dateListItem { 
    return this.originDataArray[index]; 
  } 
 
  // 该方法为框架侧调用,为LazyForEach组件向其数据源处添加listener监听 
  registerDataChangeListener(listener: DataChangeListener): void { 
    if (this.listeners.indexOf(listener) < 0) { 
      console.info('add listener'); 
      this.listeners.push(listener); 
    } 
  } 
 
  // 该方法为框架侧调用,为对应的LazyForEach组件在数据源处去除listener监听 
  unregisterDataChangeListener(listener: DataChangeListener): void { 
    const pos = this.listeners.indexOf(listener); 
    if (pos >= 0) { 
      console.info('remove listener'); 
      this.listeners.splice(pos, 1); 
    } 
  } 
 
  // 通知LazyForEach组件需要重载所有子组件 
  notifyDataReload(): void { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded(); 
    }) 
  } 
 
  // 通知LazyForEach组件需要在index对应索引处添加子组件 
  notifyDataAdd(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataAdd(index); 
    }) 
  } 
 
  // 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件 
  notifyDataChange(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataChange(index); 
    }) 
  } 
 
  // 通知LazyForEach组件需要在index对应索引处删除该子组件 
  notifyDataDelete(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataDelete(index); 
    }) 
  } 
} 
 
// 内层数据 
class MyDataSource1 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); 
  } 
} 
 
// 外层数据 
class MyDataSource2 extends BasicDataSource { 
  private dataArray: dateListItem[] = []; 
 
  public totalCount(): number { 
    return this.dataArray.length; 
  } 
 
  public getData(index: number): dateListItem { 
    return this.dataArray[index]; 
  } 
 
  public addData(index: number, data: dateListItem): void { 
    this.dataArray.splice(index, 0, data); 
    this.notifyDataAdd(index); 
  } 
 
  public pushData(data: dateListItem): void { 
    this.dataArray.push(data); 
    this.notifyDataAdd(this.dataArray.length - 1); 
  } 
} 
 
class dateListItem { 
  date: string 
  orderList: MyDataSource1 
 
  constructor(date: string) { 
    this.date = date 
    this.orderList = new MyDataSource1() 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.

注明适配的版本信息

IDE:4.1.1.300

SDK:4.1.2.1

分享
微博
QQ
微信
回复
2024-05-27 16:14:13
相关问题
ListItemGroup lazyforeach如何结合使用
1172浏览 • 1回复 待解决
ListItemGroup能跟LazyForEach搭配使用吗
1398浏览 • 1回复 待解决
HarmonyOS LazyForEach
829浏览 • 1回复 待解决
HarmonyOS Navigationrouter的使用场景
1579浏览 • 2回复 待解决
HarmonyOS onDidBuild的作用应用场景
1026浏览 • 1回复 待解决
list 如何使用 lazyforeach
951浏览 • 1回复 待解决
HarmonyOS 在@State@Prop的单向传输场景
609浏览 • 1回复 待解决
HARHSP的使用场景区分
1424浏览 • 1回复 待解决