HarmonyOS 要做一个可以无限滚动的list

目前有需求做一个列表自动滚动到底部后可以衔接到头部实现无限无缝滚动的list,如何实现?

HarmonyOS
2024-09-29 11:57:07
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

文字的横向滚动可以直接使用marquee组件,文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-marquee-V5,图像等横向滚动可以参考下面的demo,可自行加上动画animation效果:

class Item {  
  img: ResourceStr = ''  
  title?: string = ''  
}  
@Entry  
@Component  
struct Index {  
  index: number = 0  
  @State NoteLists: Array<Item> = [  
    {  
      img: $r('app.media.app_icon'),  
      title: '头像1号'  
    },  
    {  
      img: $r('app.media.app_icon'),  
      title: '头像2号'  
    },  
    {  
      img: $r('app.media.app_icon'),  
      title: '头像3号'  
    },  
    {  
      img: $r('app.media.app_icon'),  
      title: '头像4号'  
    },  
    {  
      img: $r('app.media.app_icon'),  
      title: '头像5号'  
    },  
    {  
      img: $r('app.media.app_icon'),  
      title: '头像6号'  
    }  
  ]  
  scroller: Scroller = new Scroller()  
  build() {  
    Row() {  
      Column() {  
        Scroll(this.scroller) {  
          Row() {  
            ForEach(this.NoteLists, (item: Item) => {  
              Column() {  
                Image(item.img)  
                  .width(50).height(50)  
                Text(item.title)  
              }.width(80).height(80).justifyContent(FlexAlign.SpaceBetween)  
            })  
          }  
        }.width('100%')  
        .scrollable(ScrollDirection.Horizontal)  
        .scrollBar(BarState.Off)  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
  onPageShow() {  
    setInterval(() => {  
      // 点击后滑动到指定位置,即下滑100.0vp的距离  
      if (this.index == 6) {  
        this.index = 0  
      }  
      console.log("index is:" + this.index)  
      let currentIndex: number = this.index;  
      let xOffset: number = this.scroller.currentOffset().xOffset;  
      this.NoteLists.push(this.NoteLists[currentIndex]);  
      this.index = currentIndex + 1;  
      this.scroller.scrollTo({ xOffset: xOffset + 10, yOffset: 0 })  
    }, 100)  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-09-29 18:17:00
相关问题
HarmonyOS List一个可见ListItem
1013浏览 • 1回复 待解决
HarmonyOS list最后一个显示不出来
871浏览 • 1回复 待解决