HarmonyOS IDataSource数据源刷新后,列表为不刷新

一个列表,里面有Text和一个CheckBox, 当点击一个item中的Text时,具有相同Text内容的都属于选择状态, 下拉刷新后(点击顶部刷新文字), 应具有相同的交互。 但是目前测试结果是数据刷新后点击无效。代码如下:

import { BasicDataSource } from '../BasicDataSource';  
import { MediaModel } from '../MediaModel';  
import { promptAction } from '@kit.ArkUI';  
@Component  
struct ItemView {  
  @ObjectLink model: MediaModel  
  clickCallback: ((model: MediaModel) => void) | null = null  
  build() {  
    Row() {  
      Text(this.model.name)  
        .fontColor(Color.White)  
        .onClick(() => {  
          this.clickCallback && this.clickCallback(this.model)  
        })  
        .textAlign(TextAlign.Center)  
        .width(60)  
        .height(48)  
        .backgroundColor(Color.Red)  
      Checkbox().select(this.model.isConcern)  
    }.height(64)  
  }  
}  
@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World';  
  @State dataSource: BasicDataSource<MediaModel> = new BasicDataSource()  
  concernList: MediaModel[] = []  
  aboutToAppear(): void {  
    this.refreshData()  
  }  
  build() {  
    Row() {  
      Column() {  
        Text('刷新')  
          .fontSize(16)  
          .fontWeight(FontWeight.Bold).onClick(() => {  
          this.refreshData()  
          promptAction.showToast({ message: '刷新完成' })  
        })  
        List() {  
          LazyForEach(this.dataSource, (item: MediaModel) => {  
            ListItem() {  
              ItemView({  
                model: item, clickCallback: (model: MediaModel) => {  
                  this.addConcern(model)  
                }  
              })  
            }  
          })  
        }  
      }  
      .width('100%').justifyContent(FlexAlign.Start)  
    }  
    .height('100%').alignItems(VerticalAlign.Top)  
  }  
  private refreshData() {  
    this.dataSource.clear()  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('DEF'))  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
  }  
  private addConcern(media: MediaModel) {  
    if (this.concernList.findIndex((value) => value.name == media.name) == -1) {  
      this.concernList.push(media)  
    }  
    this.filterConcern()  
  }  
  private filterConcern() {  
    for (let i = 0; i < this.dataSource.totalCount(); i++) {  
      const item = this.dataSource.getData(i)  
      item.isConcern = this.concernList.findIndex((value) => value.name == item.name) != -1  
    }  
    console.error('aaa', 'aaa')  
  }  
}  
export class BasicDataSource <T> implements IDataSource {  
  private listeners: DataChangeListener[] = [];  
  private originDataArray: T[] = [];  
  public totalCount(): number {  
    return this.originDataArray.length;  
  }  
  public pushData(data: T): void {  
    this.originDataArray.push(data);  
    this.notifyDataAdd(this.originDataArray.length - 1);  
  }  
  public clear():void{  
    this.originDataArray.splice(0, this.originDataArray.length)  
    this.notifyDataReload()  
  }  
  public getData(index: number): T {  
    return this.originDataArray[index];  
  }  
  registerDataChangeListener(listener: DataChangeListener): void {  
    if (this.listeners.indexOf(listener) < 0) {  
      console.info('add listener');  
      this.listeners.push(listener);  
    }  
  }  
  unregisterDataChangeListener(listener: DataChangeListener): void {  
    const pos = this.listeners.indexOf(listener);  
    if (pos >= 0) {  
      console.info('remove listener');  
      this.listeners.splice(pos, 1);  
    }  
  }  
  notifyDataReload(): void {  
    this.listeners.forEach(listener => {  
      listener.onDataReloaded();  
    })  
  }  
  notifyDataAdd(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataAdd(index);  
    })  
  }  
  notifyDataChange(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataChange(index);  
    })  
  }  
  notifyDataDelete(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataDelete(index);  
    })  
  }  
}  
@Observed  
export class MediaModel {  
  name: string = ''  
  isConcern: boolean = false  
  constructor(name: string) {  
    this.name = name  
  }  
}
HarmonyOS
2024-10-16 09:54:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考demo:

import { BasicDataSource } from '../BasicDataSource';  
import { MediaModel } from '../MediaModel';  
import { promptAction } from '@kit.ArkUI';  
@Component  
struct ItemView {  
  @ObjectLink model: MediaModel  
  clickCallback: ((model: MediaModel) => void) | null = null  
  build() {  
    Row() {  
      Text(this.model.name)  
        .fontColor(Color.White)  
        .onClick(() => {  
          this.clickCallback && this.clickCallback(this.model)  
        })  
        .textAlign(TextAlign.Center)  
        .width(60)  
        .height(48)  
        .backgroundColor(Color.Red)  
      Checkbox().select(this.model.isConcern)  
    }.height(64)  
  }  
}  
@Entry  
@Component  
struct Index {  
  @State message: string = 'Hello World';  
  @State dataSource: BasicDataSource<MediaModel> = new BasicDataSource()  
  concernList: MediaModel[] = []  
  aboutToAppear(): void {  
    this.refreshData()  
  }  
  build() {  
    Row() {  
      Column() {  
        Text('刷新')  
          .fontSize(16)  
          .fontWeight(FontWeight.Bold).onClick(() => {  
          this.refreshData()  
          promptAction.showToast({ message: '刷新完成' })  
        })  
        List() {  
          LazyForEach(this.dataSource, (item: MediaModel) => {  
            ListItem() {  
              ItemView({  
                model: item, clickCallback: (model: MediaModel) => {  
                  this.addConcern(model)  
                }  
              })  
            }  
          })  
        }  
      }  
      .width('100%').justifyContent(FlexAlign.Start)  
    }  
    .height('100%').alignItems(VerticalAlign.Top)  
  }  
  private refreshData() {  
    this.dataSource.clear()  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('DEF'))  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('ABC'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('XYZ'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
    this.dataSource.pushData(new MediaModel('LMN'))  
  }  
  private addConcern(media: MediaModel) {  
    if (this.concernList.findIndex((value) => value.name == media.name) == -1) {  
      this.concernList.push(media)  
    }  
    this.filterConcern()  
  }  
  private filterConcern() {  
    for (let i = 0; i < this.dataSource.totalCount(); i++) {  
      const item = this.dataSource.getData(i)  
      item.isConcern = this.concernList.findIndex((value) => value.name == item.name) != -1  
    }  
    console.error('aaa', 'aaa')  
  }  
}  
export class BasicDataSource<T> implements IDataSource {  
  private listeners: DataChangeListener[] = [];  
  private originDataArray: T[] = [];  
  public totalCount(): number {  
    return this.originDataArray.length;  
  }  
  public pushData(data: T): void {  
    this.originDataArray.push(data);  
    this.notifyDataAdd(this.originDataArray.length - 1);  
  }  
  public clear(): void {  
    this.originDataArray.splice(0, this.originDataArray.length)  
    this.notifyDataReload()  
  }  
  public getData(index: number): T {  
    return this.originDataArray[index];  
  }  
  registerDataChangeListener(listener: DataChangeListener): void {  
    if (this.listeners.indexOf(listener) < 0) {  
      console.info('add listener');  
      this.listeners.push(listener);  
    }  
  }  
  unregisterDataChangeListener(listener: DataChangeListener): void {  
    const pos = this.listeners.indexOf(listener);  
    if (pos >= 0) {  
      console.info('remove listener');  
      this.listeners.splice(pos, 1);  
    }  
  }  
  notifyDataReload(): void {  
    this.listeners.forEach(listener => {  
      listener.onDataReloaded();  
    })  
  }  
  notifyDataAdd(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataAdd(index);  
    })  
  }  
  notifyDataChange(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataChange(index);  
    })  
  }  
  notifyDataDelete(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataDelete(index);  
    })  
  }  
}  
@Observed  
export class MediaModel {  
  name: string = ''  
  isConcern: boolean = false  
  constructor(name: string) {  
    this.name = name  
  }  
}
分享
微博
QQ
微信
回复
2024-10-16 15:58:24
相关问题
HarmonyOS使用@ObjectLink 数据刷新
798浏览 • 2回复 待解决
IF条件变化UI刷新
783浏览 • 1回复 待解决
lazyforeach替换数据源解决方案
740浏览 • 1回复 待解决
HarmonyOS 深色模式切换界面刷新
36浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
659浏览 • 1回复 待解决
HarmonyOS UI刷新
30浏览 • 1回复 待解决
HarmonyOS 通知列表刷新事件
75浏览 • 1回复 待解决
HarmonyOS ForEach列表刷新问题
79浏览 • 1回复 待解决
HarmonyOS picker选择器的数据源问题
0浏览 • 0回复 待解决
修改ForEach使用的数据对象,UI刷新
1875浏览 • 1回复 待解决