HarmonyOS List列表滚动到指定位置

使用list加载一个数据,比如首次加载20条数据加载完成自动滚动列表底部,可往上滚动加载,滚定指定位加载数据。

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

参考示例如下:

@Entry
@Component
struct RefreshExample {
  @State isRefreshing: boolean = false
  @State arr: String[] = ['90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100']
  private scrollerForList: Scroller = new Scroller()
  count: number = 89

  build() {
    Column() {
      Refresh({ refreshing: $$this.isRefreshing, offset: 0 }) {
        List({ initialIndex: this.arr.length - 1, scroller: this.scrollerForList }) {
          ForEach(this.arr, (item: string) => {
            ListItem() {
              Text('' + item)
                .width('100%')
                .height(100)
                .fontSize(16)
                .textAlign(TextAlign.Center)
                .borderRadius(10)
                .backgroundColor(0xFFFFFF)
            }
          }, (item: string) => item)
        }
        .onScrollIndex((first: number) => {
          console.info(first.toString())
        })
        .width('100%')
        .height('100%')
        .divider({
          strokeWidth: 1,
          color: Color.Yellow,
          startMargin: 10,
          endMargin: 10
        })
        .scrollBar(BarState.Off)
      }
      .onStateChange((refreshStatus: RefreshStatus) => {
        this.arr.unshift(this.count-- + '')
        console.info('Refresh onStatueChange state is ' + refreshStatus)
      })
      .onOffsetChange((value: number) => {
        console.info('Refresh onOffsetChange offset:' + value)
      })
      .onRefreshing(() => {
        this.scrollerForList.scrollToIndex(4, false)
        setTimeout(() => {
          this.isRefreshing = false
        }, 500)
        console.log('onRefreshing test')
      })
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
list组件无法滚动到底部
1307浏览 • 1回复 待解决