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 数据刷新
583浏览 • 2回复 待解决
IF条件变化UI刷新
651浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
472浏览 • 1回复 待解决
lazyforeach替换数据源解决方案
614浏览 • 1回复 待解决
修改ForEach使用的数据对象,UI刷新
1463浏览 • 1回复 待解决
刷新列表加载更多问题
240浏览 • 1回复 待解决
HarmonyOS 下面demo为什么点击刷新
254浏览 • 1回复 待解决
HarmonyOS 状态变量刷新问题
331浏览 • 1回复 待解决
状态装饰器 ui刷新的问题
2357浏览 • 1回复 待解决