HarmonyOS waterFlow里面是否可以嵌套一个横向滑动的list

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

1.用waterflow套flowitem+lazyforeach,然后flowitem里面套list

2.用scroll套list和waterflow来写,参考下方demo实现

// Index.ets
import { WaterFlowDataSource } from './WaterFlowDataSource'

@Entry
@Component
struct WaterFlowDemo {
  @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[] = []
  private arr: number[] = []
  private scrollerForList: Scroller = new Scroller()
  @State hitModel: HitTestMode = HitTestMode.Default
  // 计算FlowItem宽/高
  getSize() {
    let ret = Math.floor(Math.random() * this.maxSize)
    return (ret > this.minSize ? ret : this.minSize)
  }

  // 设置FlowItem的宽/高数组
  setItemSizeArray() {
    for (let i = 0; i < 100; i++) {
      this.itemWidthArray.push(this.getSize())
      this.itemHeightArray.push(this.getSize())
    }
  }

  aboutToAppear() {
    for (let i = 0; i < 20; i++) {
      this.arr.push(i)
    }
    this.setItemSizeArray()
  }

  @Builder
  itemFoot() {
    Column() {
      Text(`Footer`)
        .fontSize(10)
        .backgroundColor(Color.Red)
        .width(50)
        .height(50)
        .align(Alignment.Center)
        .margin({ top: 2 })
    }
  }

  build() {
    Scroll(){
      Column({ space: 2 }) {
        Row() {
          List({ space: 20, initialIndex: 3, scroller: this.scrollerForList }) {
            ForEach(this.arr, (item: number) => {
              ListItem() {
                Text('' + item)
                  .width('100%').height(100).fontSize(16)
                  .textAlign(TextAlign.Center)
              }
              .borderRadius(10).backgroundColor(0xFFFFFF)
              .width('60%')
              .height('80%')
            }, (item: number) => JSON.stringify(item))
          }
          .chainAnimation(true)
          .edgeEffect(EdgeEffect.Spring)
          .listDirection(Axis.Horizontal)
          .height(100)
          .width(100)
          .scrollSnapAlign(ScrollSnapAlign.CENTER)
          .borderRadius(10)
          .backgroundColor(0xDCDCDC)
        }
        .backgroundColor(0xDCDCDC)
        .padding({ top: 10 })

        WaterFlow() {
          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('100%')
        .height('100%')
        .onReachStart(() => {
          console.info('waterFlow reach start')
        })
        .onScrollStart(() => {
          console.info('waterFlow scroll start')
        })
        .onScrollStop(() => {
          console.info('waterFlow scroll stop')
        })
        .onScrollFrameBegin((offset: number, state: ScrollState) => {
          console.info('waterFlow scrollFrameBegin offset: ' + offset + ' state: ' + state.toString())
          return { offsetRemain: offset }
        })
        .nestedScroll( { scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward:NestedScrollMode.SELF_FIRST, })
      }
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS List嵌套waterflow滑动卡顿
301浏览 • 1回复 待解决
Scroll与WaterFlow滑动嵌套
1079浏览 • 1回复 待解决
HarmonyOS 要做一个可以无限滚动list
699浏览 • 1回复 待解决
scroll和list嵌套滑动
1546浏览 • 1回复 待解决
HarmonyOS scroll嵌套List不能整体滑动
516浏览 • 1回复 待解决
HarmonyOS list 嵌套web滑动切换问题
509浏览 • 1回复 待解决
JS如可开发一个横向拖动表格
6382浏览 • 1回复 待解决
是否可以申明一个静态map并使用?
195浏览 • 1回复 待解决