HarmonyOS List组件和WaterFlow组件增强

项目需要大量使用List组件和WaterFlow组件,外层还要嵌套ScrollView才能实现页面的整体滚动,但滑动冲突又很难解决;

HarmonyOS
2024-08-04 18:01:09
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

如果是滑动嵌套的话官网有很多示例的如果是滑动冲突的问题请参考nestedScroll属性,文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-waterflow-0000001815767844#ZH-CN_TOPIC_0000001815767844__属性

示例:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-container-scroll-0000001815927632#ZH-CN_TOPIC_0000001815927632__示例3

list外层嵌套scroll:https://developer.huawei.com/consumer/cn/doc/harmonyos-samples/samples-0000001162414961waterFlow嵌套滑动:

//index.ets 
import { WaterFlowDataSource } from './WaterFlowDataSource' 
@Entry 
@Component 
struct Index { 
  @State Number: String[] = ['0', '1', '2', '3', '4'] 
  @State minSize: number = 80 
  @State maxSize: number = 180 
  @State fontSize: number = 24 
  @State colors: number[] = [0xFFC0CB, 0xDA70D6, 0x6B8E23, 0x6A5ACD, 0x00FFFF, 0x00FF7F] 
  scroller: Scroller = new Scroller() 
  dataSource: WaterFlowDataSource = new WaterFlowDataSource() 
  private itemWidthArray: number[] = [] 
  private itemHeightArray: number[] = [] 
  // 计算flow item宽/高 
  getSize() { 
    let ret = Math.floor(Math.random() * this.maxSize) 
    return (ret > this.minSize ? ret : this.minSize) 
  } 
  // 保存flow item宽/高 
  getItemSizeArray() { 
    for (let i = 0; i < 100; i++) { 
      this.itemWidthArray.push(this.getSize()) 
      this.itemHeightArray.push(this.getSize()) 
    } 
  } 
  aboutToAppear() { 
    this.getItemSizeArray() 
  } 
  @Builder 
  itemFoot() { 
    Column() { 
      Text(`Footer`) 
        .fontSize(10) 
        .backgroundColor(Color.Red) 
        .width(50) 
        .height(50) 
        .align(Alignment.Center) 
        .margin({ top: 2 }) 
    } 
  } 
  build() { 
    Column() { 
      Scroll(this.scroller) { 
        Column({space:10}) { 
          Grid() { 
            ForEach(this.Number, (day: string) => { 
              ForEach(this.Number, (day: string) => { 
                GridItem() { 
                  Text(day) 
                    .fontSize(16) 
                    .backgroundColor(0xF9CF93) 
                    .width('100%') 
                    .height('100%') 
                    .textAlign(TextAlign.Center) 
                } 
              }) 
            }) 
          } 
          .columnsTemplate('1fr 1fr 1fr 1fr') 
          .rowsTemplate('1fr 1fr') 
          .columnsGap(10) 
          .rowsGap(10) 
          .width('90%') 
          .backgroundColor(0xFAEEE0) 
          .height(140) 
 
          Swiper(new SwiperController()) { 
            ForEach(["测试1", "测试2", "测试3", "测试4", "测试5", "测试6"], (item: string) => { 
              Text(item).fontSize(20) 
            }) 
          }.autoPlay(true) 
          .vertical(false) 
          .indicator(false) 
          .loop(true) 
          .width('90%') 
          .backgroundColor(Color.Pink) 
          .height(100) 
 
          WaterFlow({scroller:this.scroller}) { 
            LazyForEach(this.dataSource, (item: number) => { 
              FlowItem() { 
                Column() { 
                  Text("N" + item).fontSize(12).height('16') 
                  Image('res/waterFlowTest(' + item % 5 + ').jpg') 
                    .objectFit(ImageFit.Fill) 
                    .width('100%') 
                    .layoutWeight(1) 
                } 
              } 
              .onAppear(() => { 
                // 即将触底时提前增加数据 
                if (item + 20 == this.dataSource.totalCount()) { 
                  for (let i = 0; i < 100; i++) { 
                    this.dataSource.addLastItem() 
                  } 
                } 
              }) 
              .width('100%') 
              .height(this.itemHeightArray[item % 100]) 
              .backgroundColor(this.colors[item % 5]) 
            }, (item: string) => item) 
          } 
          .columnsTemplate("1fr 1fr") 
          .columnsGap(10) 
          .rowsGap(5) 
          .backgroundColor(0xFAEEE0) 
          .width('90%') 
          .height('100%') 
          .nestedScroll({ 
            scrollForward: NestedScrollMode.PARENT_FIRST, 
            scrollBackward: NestedScrollMode.SELF_FIRST 
          }) 
 
        }.width('100%').backgroundColor(0xDCDCDC) 
      } 
      .backgroundColor(Color.Yellow) 
      .height('100%') 
      .edgeEffect(EdgeEffect.Spring) 
 
    } 
  } 
} 
 
//WaterFlowDataSource.ets 
export class WaterFlowDataSource implements IDataSource { 
  private dataArray: number[] = [] 
  private listeners: DataChangeListener[] = [] 
  constructor() { 
    for (let i = 0; i < 100; i++) { 
      this.dataArray.push(i) 
    } 
  } 
  // 获取索引对应的数据 
  public getData(index: number): number { 
    return this.dataArray[index] 
  } 
  // 通知控制器数据重新加载 
  notifyDataReload(): void { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded() 
    }) 
  } 
  // 通知控制器数据增加 
  notifyDataAdd(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataAdded(index) 
    }) 
  } 
  // 通知控制器数据变化 
  notifyDataChange(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataChanged(index) 
    }) 
  } 
  // 通知控制器数据删除 
  notifyDataDelete(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataDeleted(index) 
    }) 
  } 
  // 通知控制器数据位置变化 
  notifyDataMove(from: number, to: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataMoved(from, to) 
    }) 
  } 
  // 获取数据总数 
  public totalCount(): number { 
    return this.dataArray.length 
  } 
  // 注册改变数据的控制器 
  registerDataChangeListener(listener: DataChangeListener): void { 
    if (this.listeners.indexOf(listener) < 0) { 
      this.listeners.push(listener) 
    } 
  } 
  // 注销改变数据的控制器 
  unregisterDataChangeListener(listener: DataChangeListener): void { 
    const pos = this.listeners.indexOf(listener) 
    if (pos >= 0) { 
      this.listeners.splice(pos, 1) 
    } 
  } 
  // 增加数据 
  public add1stItem(): void { 
    this.dataArray.splice(0, 0, this.dataArray.length) 
    this.notifyDataAdd(0) 
  } 
  // 在数据尾部增加一个元素 
  public addLastItem(): void { 
    this.dataArray.splice(this.dataArray.length, 0, this.dataArray.length) 
    this.notifyDataAdd(this.dataArray.length - 1) 
  } 
  // 在指定索引位置增加一个元素 
  public addItem(index: number): void { 
    this.dataArray.splice(index, 0, this.dataArray.length) 
    this.notifyDataAdd(index) 
  } 
  // 删除第一个元素 
  // 删除第一个元素 
  public delete1stItem(): void { 
    this.dataArray.splice(0, 1) 
    this.notifyDataDelete(0) 
  } 
  // 删除第二个元素 
  public delete2ndItem(): void { 
    this.dataArray.splice(1, 1) 
    this.notifyDataDelete(1) 
  } 
  // 删除最后一个元素 
  public deleteLastItem(): void { 
    this.dataArray.splice(-1, 1) 
    this.notifyDataDelete(this.dataArray.length) 
  } 
  // 重新加载数据 
  public reload(): void { 
    this.dataArray.splice(1, 1) 
    this.dataArray.splice(3, 2) 
    this.notifyDataReload() 
  } 
}
  • 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.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
分享
微博
QQ
微信
回复
2024-08-05 12:38:08
相关问题
HarmonyOS List嵌套waterflow滑动卡顿
912浏览 • 1回复 待解决
HarmonyOS Web组件List的嵌套使用问题
1206浏览 • 1回复 待解决
WaterFlow组件如何实现拖拽交换功能
188浏览 • 1回复 待解决
List组件divider颜色显示透List组件颜色
735浏览 • 0回复 待解决
HarmonyOS list组件问题
678浏览 • 1回复 待解决
HarmonyOS List组件不能嵌套Grid组件
571浏览 • 1回复 待解决
HarmonyOS 监听List组件滑动
824浏览 • 1回复 待解决
HarmonyOS 设置list组件高度
494浏览 • 1回复 待解决
HarmonyOS LIst组件UI不刷新
489浏览 • 1回复 待解决
HarmonyOS List组件的滚动监听
723浏览 • 1回复 待解决
HarmonyOS list控件子组件复用
1140浏览 • 1回复 待解决
HarmonyOS 组件List如何禁止滑动
1254浏览 • 1回复 待解决
HarmonyOS List组件沉浸式问题
667浏览 • 1回复 待解决