HarmonyOS 如何实现里面的是列表,翻页每次都是10个item

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

可以通过swiper和grid来实现,参考示例如下:

// xxx.ets
class MyDataSource implements IDataSource {
  private list: number[] = []

  constructor(list: number[]) {
    this.list = list
  }

  totalCount(): number {
    return this.list.length
  }

  getData(index: number): number {
    return this.list[index]
  }

  registerDataChangeListener(listener: DataChangeListener): void {
  }

  unregisterDataChangeListener() {
  }
}

@Entry
@Component
struct SwiperExample {
  private swiperController: SwiperController = new SwiperController()
  private data: MyDataSource = new MyDataSource([])
  @State numbers: String[] = ['0', '1', '2', '3']
  scroller: Scroller = new Scroller()
  @State gridPosition: number = 0 //0代表滚动到grid顶部,1代表中间值,2代表滚动到grid底部。

  aboutToAppear(): void {
    let list: number[] = []
    for (let i = 1; i <= 10; i++) {
      list.push(i);
    }
    this.data = new MyDataSource(list)
  }

  build() {
    Column({ space: 5 }) {
      Swiper(this.swiperController) {
        LazyForEach(this.data, (item: string) => {
          Grid(this.scroller) {
            ForEach(this.numbers, (day: string) => {
              ForEach(this.numbers, (day: string) => {
                GridItem() {
                  Text(day)
                    .fontSize(16)
                    .backgroundColor(0xF9CF93)
                    .width('100%')
                    .height(60)
                    .textAlign(TextAlign.Center)
                    .margin({ left: 5, right: 5 })
                }
              }, (day: string) => day)
            }, (day: string) => day)
          }
          .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
          .rowsTemplate('1fr 1fr ')
          .columnsGap(10)
          .rowsGap(10)
          .friction(0.6)
          .enableScrollInteraction(false)

          .supportAnimation(false)
          .multiSelectable(false)
          .edgeEffect(EdgeEffect.Spring)
          .scrollBar(BarState.Off)
          .width('90%')
          .backgroundColor(0xFAEEE0)
          .height(220)
          .onScrollIndex((first: number, last: number) => {
            console.info(first.toString())
            console.info(last.toString())
          })
          .onScrollBarUpdate((index: number, offset: number) => {
            console.info("XXX" + 'Grid onScrollBarUpdate,index : ' + index.toString() + ",offset" + offset.toString())
            return { totalOffset: (index / 5) * (80 + 10) - offset, totalLength: 80 * 5 + 10 * 4 }
          }) //只适用于当前示例代码数据源,如果数据源有变化,则需要修改该部分代码,或者删掉此属性
          .onDidScroll((scrollOffset: number, scrollState: ScrollState) => {
            console.info(scrollOffset.toString())
            console.info(scrollState.toString())
          })
          .onScrollStart(() => {
            console.info("XXX" + "Grid onScrollStart")
          })
          .onScrollStop(() => {
            console.info("XXX" + "Grid onScrollStop")
          })
          .onReachStart(() => {
            this.gridPosition = 0
            console.info("XXX" + "Grid onReachStart")
          })
          .onReachEnd(() => {
            this.gridPosition = 2
            console.info("XXX" + "Grid onReachEnd")
          })

        }, (item: string) => item)
      }
      .cachedCount(2)
      .index(1)
      .autoPlay(true)
      .interval(4000)
      .loop(true)
      .indicatorInteractive(true)
      .duration(1000)
      .itemSpace(0)
      .curve(Curve.Linear)
      .onChange((index: number) => {
        console.info(index.toString())
      })
      .onGestureSwipe((index: number, extraInfo: SwiperAnimationEvent) => {
        console.info("index: " + index)
        console.info("current offset: " + extraInfo.currentOffset)
      })
      .onAnimationStart((index: number, targetIndex: number, extraInfo: SwiperAnimationEvent) => {
        console.info("index: " + index)
        console.info("targetIndex: " + targetIndex)
        console.info("current offset: " + extraInfo.currentOffset)
        console.info("target offset: " + extraInfo.targetOffset)
        console.info("velocity: " + extraInfo.velocity)
      })
      .onAnimationEnd((index: number, extraInfo: SwiperAnimationEvent) => {
        console.info("index: " + index)
        console.info("current offset: " + extraInfo.currentOffset)
      })

    }.width('100%')
    .margin({ top: 5 })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS Swiper里面的item高度自动刷新
485浏览 • 1回复 待解决
HarmonyOS grid里面的item支持拖动问题
65浏览 • 1回复 待解决
HarmonyOS 每次编译都需要10分钟
82浏览 • 1回复 待解决
HarmonyOS 如何实现数据翻页效果?
286浏览 • 1回复 待解决
HarmonyOS中List如何加载item的?
244浏览 • 2回复 待解决
如何查看mysql表里面的数据?
2487浏览 • 1回复 待解决
HarmonyOS如何实现阅读器翻页效果?
412浏览 • 1回复 待解决
怎么获取List里面每个item的position?
297浏览 • 1回复 待解决
HarmonyOS 类似翻页效果实现
82浏览 • 1回复 待解决
如何实现翻页功能,有人知道吗?
1989浏览 • 1回复 待解决