HarmonyOS 局部刷新,网络请求回数据后,刷新某个值

List中的某个ListItem,ListItem中有swiper,swiper里面有4个view,每个view都是一个List,forEach循环渲染。

这个ListItem的数据间隔10秒从网络重新请求。

如果请求完的数据,直接赋值给老的数据的话:

1.ListItem中有swiper重新创建,后面的view全部重新创建,比较耗费性能。

2.ListItem中有swiper不重新创建,swiper绑定的是之前的数据,新数据赋值过去更改的话,UI也不会刷新。

最好的办法,就是对比2个对象,把新对象的数据都赋值给老的对象。或者是有变动的值赋值给老的对象,这种需求应该怎么实现呢?

对象如下:

@ObservedV2  
export class MergeCellData implements CellType {  
  cardData?: CardData;  
  itemType: ItemType;  
  key?: string;  
  @Trace items: Array<SubPickData | SubEvaluateData> = new Array();  
  titles: string[] = [];  
  }  
}  
@ObservedV2  
export class SubPickData  {  
  subCardData: SubCardData;  
  @Trace cell: Array<MoneyPickData|NewsPickData|BasicPickData|TechnologyPickData> = new Array();  
  constructor(subCardData: SubCardData,cell: Array<MoneyPickData|NewsPickData|BasicPickData|TechnologyPickData>) {  
    this.subCardData = subCardData;  
    this.cell = cell;  
  }  
}  
@ObservedV2  
export class MoneyPickData extends BaseChartPickData {  
  @Trace downCount: number;  
  @Trace flatCount: number;  
  @Trace fundCode: string;  
  @Trace fundJumpUrl: string;  
  @Trace fundName: string;  
  @Trace plateChangeRate: string;  
  @Trace plateDesc: string;  
  @Trace plateFundFlow: string;  
  @Trace plateFundFlowDesc: string;  
  @Trace plateFundName: string;  
  @Trace plateHisFundDesc: string;  
  @Trace plateJumpUrl: string;  
  @Trace plateName: string;  
  @Trace upCount: number;  
  @Trace topStocks: StockChgData[];  
}
  • 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.
HarmonyOS
2024-10-16 11:45:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考代码:

@Component  
struct Child {  
  @Link items: TestList[];  
  build() {  
    Column() {  
      ForEach(this.items, (item: TestList) => {  
        ChildChild({ item: item });  
      })  
      Button(Button:  
      push  
      )  
      .onClick(() => {  
        this.items[0].index1 = 3;  
        this.items[0].str = 'Third';  
      })  
    }  
  }  
}  
@Component  
struct ChildChild {  
  @State item: TestList = new TestList();  
  build() {  
    Column() {  
      Text($  
      {  
        this.item.index1  
      }  
      +'-----'  
      $  
      {  
        this.item.str  
      }  
      )  
    }  
  }  
}  
  
@Entry  
@Component  
struct Parent {  
  @State arr: TestList[] = [];  
  aboutToAppear() {  
    let test1: TestList = new TestList();  
    test1.index1 = 1;  
    test1.str = 'First';  
    this.arr.push(test1)  
    let test2: TestList = new TestList();  
    test2.index1 = 2;  
    test2.str = 'Second';  
    this.arr.push(test2)  
  }  
  build() {  
    Column() {  
      Child({ items: this.arr })  
      Button(Button:  
      push  
      )  
      .onClick(() => {  
        this.arr[1].index1 = 4;  
        this.arr[1].str = 'Fourth';  
      })  
    }  
  }  
}  
@Observed  
class TestList {  
  index1: number = 0;  
  str: string = '';  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-16 16:58:22


相关问题
HarmonyOS ForEach局部刷新
1057浏览 • 1回复 待解决
HarmonyOS list局部刷新的问题
254浏览 • 1回复 待解决
HarmonyOS中List组件是否支持局部刷新
949浏览 • 1回复 待解决
HarmonyOS 更新数据UI不刷新
552浏览 • 1回复 待解决
网络请求如何进行调?
1597浏览 • 1回复 待解决
List局部刷新,有人知道怎么处理吗?
1624浏览 • 1回复 待解决
网络请求数据的处理
895浏览 • 1回复 待解决
HarmonyOS List怎么刷新数据
457浏览 • 1回复 待解决
HarmonyOS 数据刷新问题
418浏览 • 1回复 待解决
HarmonyOS 数组对象数据刷新
546浏览 • 1回复 待解决