HarmonyOS 下拉刷新的时候没有更新数据

现在有个场景,就是下拉刷新之后,调用接口的数据,成功之后,替换了本地的dataSource。但是数据却没有更新。替换的代码如下:LazyDataSource里的pushArrayData。

public pushArrayData(newData: ObservedArray<T>): void {  
  this.clear();  
  this.dataArray.push(...newData);  
  this.notifyDataReload();  
}

发现该下拉刷新之后,数据变化了。但是列表却没有更新。

HarmonyOS
2024-10-16 10:10:42
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

可以参考以下demo排查数据替换用法是否正确。参考来源:

LazyForEach:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5#首次渲染

Refresh:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-refresh-V5#示例1

demo:

class BasicDataSource implements IDataSource {  
  private listeners: DataChangeListener[] = [];  
  private originDataArray: string[] = [];  
  public totalCount(): number {  
    return 0;  
  }  
  public getData(index: number): string {  
    return this.originDataArray[index];  
  }  
  // 该方法为框架侧调用,为LazyForEach组件向其数据源处添加listener监听  
  registerDataChangeListener(listener: DataChangeListener): void {  
    if (this.listeners.indexOf(listener) < 0) {  
      console.info('add listener');  
      this.listeners.push(listener);  
    }  
  }  
  // 该方法为框架侧调用,为对应的LazyForEach组件在数据源处去除listener监听  
  unregisterDataChangeListener(listener: DataChangeListener): void {  
    const pos = this.listeners.indexOf(listener);  
    if (pos >= 0) {  
      console.info('remove listener');  
      this.listeners.splice(pos, 1);  
    }  
  }  
  // 通知LazyForEach组件需要重载所有子组件  
  notifyDataReload(): void {  
    this.listeners.forEach(listener => {  
      listener.onDataReloaded();  
    })  
  }  
  // 通知LazyForEach组件需要在index对应索引处添加子组件  
  notifyDataAdd(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataAdd(index);  
    })  
  }  
  // 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件  
  notifyDataChange(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataChange(index);  
    })  
  }  
  // 通知LazyForEach组件需要在index对应索引处删除该子组件  
  notifyDataDelete(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataDelete(index);  
    })  
  }  
  // 通知LazyForEach组件将from索引和to索引处的子组件进行交换  
  notifyDataMove(from: number, to: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataMove(from, to);  
    })  
  }  
}  
class MyDataSource extends BasicDataSource {  
  private dataArray: string[] = [];  
  public totalCount(): number {  
    return this.dataArray.length;  
  }  
  public getData(index: number): string {  
    return this.dataArray[index];  
  }  
  public addData(index: number, data: string): void {  
    this.dataArray.splice(index, 0, data);  
    this.notifyDataAdd(index);  
  }  
  public pushData(data: string): void {  
    this.dataArray.push(data);  
    this.notifyDataAdd(this.dataArray.length - 1);  
  }  
  public reloadData(dataList:string[]):void {  
    this.dataArray.splice(0,this.dataArray.length)  
    dataList.forEach((data:string)=>{  
      this.dataArray.push(data)  
    })  
    this.notifyDataReload()  
  }  
}  
@Entry  
@Component  
struct MyComponent {  
  private data: MyDataSource = new MyDataSource();  
  @State isRefreshing: boolean = false  
  @State refreshTime: number = 1  
  aboutToAppear() {  
    for (let i = 0; i <= 10; i++) {  
      this.data.pushData(Hello ${i})  
    }  
  }  
  build() {  
    Column() {  
      Refresh({ refreshing: $$this.isRefreshing }) {  
        List() {  
          LazyForEach(this.data, (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) => {  
        console.info('Refresh onStatueChange state is ' + refreshStatus)  
      })  
      .onRefreshing(() => {  
        setTimeout(() => {  
          let newData: string[] = []  
          for (let i = 0; i <= 10; i++) {  
            newData.push(new ${i} refresh time ${this.refreshTime})  
          }  
          this.refreshTime++;  
          this.data.reloadData(newData)  
          this.isRefreshing = false  
        }, 2000)  
        console.log('onRefreshing test')  
      })  
      .backgroundColor(0x89CFF0)  
      .refreshOffset(64)  
      .pullToRefresh(true)  
    }  
  }  
}
分享
微博
QQ
微信
回复
2024-10-16 18:29:14
相关问题
HarmonyOS 组件下拉刷新问题
365浏览 • 1回复 待解决
HarmonyOS 下拉刷新功能
137浏览 • 1回复 待解决
HarmonyOS 首页下拉刷新异常
167浏览 • 1回复 待解决
HarmonyOS使用Refresh下拉刷新问题
665浏览 • 1回复 待解决
怎么在进度条更新时候刷新页面?
4584浏览 • 1回复 待解决
HarmonyOS ArkWeb如何实现下拉刷新功能
368浏览 • 1回复 待解决
上拉加载,下拉刷新组件
312浏览 • 1回复 待解决
Refresh结合lottie实现下拉刷新动画
1029浏览 • 1回复 待解决
如何使用Swiper组件实现下拉刷新
711浏览 • 1回复 待解决
下拉刷新和上拉加载API为9sdk
2720浏览 • 1回复 待解决
有谁知道web组件如何下拉刷新
1919浏览 • 1回复 待解决