HarmonyOS 懒加载的数据删除问题

官网提供的懒加载数据删除,都是在ListItem onClick 事件中触发删除,测试也没问题。

但是我的需求是在对应子组件触发删除,就出现混乱了,子组件无法拿到变更后的index,导致删除非预期数据。

请提供一下通过子组件删除 LazyForEach 对应数据的示例。

HarmonyOS
2024-09-03 10:55:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考以下demo:

Demo.ets

import { CommonDataSource } from './CommonDataSource' 
import { AlertDialog } from '@ohos.ArkUI.advanced.Dialog' 
 
class ListItemData { 
  id: string = '' 
  text: string = '' 
} 
 
@Component 
struct AddressItem { 
  item: ListItemData = new ListItemData() 
  onDeleteItem: (isDelete: boolean) => void = (isDelete: boolean) => { 
  } 
  dialogControllerConfirm: CustomDialogController = new CustomDialogController({ 
    builder: AlertDialog({ 
      content: '确定删除?', 
      primaryButton: { 
        value: '取消', 
        action: () => { 
          this.onDeleteItem(false) 
        }, 
      }, 
      secondaryButton: { 
        value: '确认', 
        fontColor: Color.Green, 
        action: () => { 
          this.onDeleteItem(true) 
        } 
      }, 
    }), 
    autoCancel: false, 
    customStyle: true, 
    alignment: DialogAlignment.Center 
  }) 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.item.text) 
        Text('点击删除') 
          .height(40) 
          .borderRadius(20) 
          .fontColor(Color.White) 
          .backgroundColor('#007dff') 
          .onClick(() => { 
            this.dialogControllerConfirm.open() 
          }) 
      }.width('100%').backgroundColor(Color.White) 
    } 
    .width('100%') 
    .constraintSize({ maxWidth: '100%' }) 
    .margin({ left: 20, right: 10 }) 
  } 
} 
 
 
 
@Entry 
@Component 
struct ListDemo { 
  // 列表数据源 
  private dataSource = new CommonDataSource<ListItemData>() 
 
  aboutToAppear(): void { 
    let arr: ListItemData[] = [] 
    for (let i = 0; i < 20; i++) { 
      let item = new ListItemData() 
      item.id = 'id_' + i 
      item.text = 'text_' + i 
      arr.push(item) 
    } 
    this.dataSource.setData(arr) 
  } 
 
  build() { 
    Column() { 
      List({ space: 20 }) { 
        LazyForEach(this.dataSource, (item: ListItemData, index: number) => { 
          ListItem() { 
            AddressItem({ 
              item: item, 
              onDeleteItem: (isDelete: boolean) => { 
                if (isDelete) { 
                  this.dataSource.deleteData(this.dataSource.originDataArray.indexOf(item)) 
                } 
              } 
            }) 
          } 
        }) 
      } 
      .listDirection(Axis.Vertical) 
      .margin({ top: 20 }) 
    } 
    .backgroundColor(0xDCDCDC) 
    .width('100%') 
    .height('100%') 
  } 
}
//数据源 
export class CommonDataSource<T> implements IDataSource { 
  private listeners: DataChangeListener[] = []; 
  originDataArray: T[] = []; 
 
  totalCount(): number { 
    return this.originDataArray.length; 
  } 
 
  getAllData(): T[] { 
    return this.originDataArray 
  } 
 
  getData(index: number) { 
    return this.originDataArray[index]; 
  } 
 
  addData(index: number, data: T): void { 
    this.originDataArray.splice(index, 0, data); 
    this.notifyDataAdd(index); 
  } 
 
  pushData(data: T): void { 
    this.originDataArray.push(data); 
    this.notifyDataAdd(this.originDataArray.length - 1); 
  } 
 
  pushDataArray(...items: T[]): void { 
    for (let data of items) { 
      this.originDataArray.push(data); 
      this.notifyDataAdd(this.originDataArray.length - 1); 
    } 
  } 
 
  deleteData(index: number): void { 
    this.originDataArray.splice(index, 1); 
    this.notifyDataDelete(index); 
  } 
 
  setData(dataArray?: T[]) { 
    if (dataArray) { 
      this.originDataArray = dataArray 
    } else { 
      this.originDataArray = [] 
    } 
    this.notifyDataReload() 
  } 
 
  registerDataChangeListener(listener: DataChangeListener): void { 
    if (this.listeners.indexOf(listener) < 0) { 
      this.listeners.push(listener); 
    } 
  } 
 
  unregisterDataChangeListener(listener: DataChangeListener): void { 
    const pos = this.listeners.indexOf(listener); 
    if (pos >= 0) { 
      this.listeners.splice(pos, 1); 
    } 
  } 
 
  notifyDataReload() { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded(); 
    }) 
  } 
 
  notifyDataAdd(index: number) { 
    this.listeners.forEach(listener => { 
      listener.onDataAdd(index); 
    }) 
  } 
 
  notifyDataMove(from: number, to: number) { 
    this.listeners.forEach(listener => { 
      listener.onDataMove(from, to); 
    }) 
  } 
 
  notifyDataDelete(index: number) { 
    this.listeners.forEach(listener => { 
      listener.onDataDelete(index); 
    }) 
  } 
 
  notifyDataChange(index: number) { 
    this.listeners.forEach(listener => { 
      listener.onDataChange(index); 
    }) 
  } 
}
分享
微博
QQ
微信
回复
2024-09-03 15:05:00
相关问题
HarmonyOS tabContent加载问题
0浏览 • 1回复 待解决
Tabs组件加载问题
2325浏览 • 1回复 待解决
grid组件及数据加载
996浏览 • 1回复 待解决
HarmonyOS 列表展示list加载问题
657浏览 • 1回复 待解决
HarmonyOS 加载
36浏览 • 1回复 待解决
使用LazyForEach加载列表相关问题
887浏览 • 1回复 待解决
LazyForEach加载原理是什么
2229浏览 • 1回复 待解决
如何实现Fraction加载功能?
7454浏览 • 1回复 待解决
HarmonyOS list数据加载问题
8浏览 • 1回复 待解决
数组嵌套数组场景加载实现
645浏览 • 1回复 待解决
界面内容瀑布流加载实现
1045浏览 • 1回复 待解决
在鸿蒙中如何实现页面的加载?
170浏览 • 0回复 待解决