温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!

HarmonyOS NEXT系列教程之列表交换组件数据模型设计

效果演示

1. 数据模型基础

1.1 基础结构

@Observed
export class ListInfo {
    // 列表项图标
    icon: ResourceStr = '';
    
    // 列表项名称
    name: ResourceStr = '';

    constructor(icon: ResourceStr = '', name: ResourceStr = '') {
        this.icon = icon;
        this.name = name;
    }
}

1.2 响应式设计

// 使用@Observed装饰器实现响应式
@Observed
export class ListInfo {
    // 当属性变化时,自动触发UI更新
}

2. 属性定义

2.1 资源类型

// 使用ResourceStr类型表示资源字符串
icon: ResourceStr = '';  // 图标资源
name: ResourceStr = '';  // 名称资源

// 资源使用示例
const item = new ListInfo(
    $r("app.media.icon_example"),  // 图标资源
    $r("app.string.name_example")  // 文本资源
);

2.2 默认值处理

constructor(
    icon: ResourceStr = '',    // 默认空字符串
    name: ResourceStr = ''     // 默认空字符串
) {
    this.icon = icon;
    this.name = name;
}

3. 响应式机制

3.1 数据监听

@Observed
export class ListInfo {
    // 属性变化时自动触发更新
    private updateIcon(newIcon: ResourceStr) {
        this.icon = newIcon;
        // 自动触发UI更新
    }

    private updateName(newName: ResourceStr) {
        this.name = newName;
        // 自动触发UI更新
    }
}

3.2 使用场景

// 创建列表项
const item = new ListInfo($r("app.media.icon1"), $r("app.string.name1"));

// 更新属性会自动触发UI更新
item.icon = $r("app.media.icon2");
item.name = $r("app.string.name2");

4. 数据验证

4.1 类型检查

class ListInfoValidator {
    static validateIcon(icon: ResourceStr): boolean {
        return typeof icon === 'string' || icon instanceof Resource;
    }

    static validateName(name: ResourceStr): boolean {
        return typeof name === 'string' || name instanceof Resource;
    }
}

4.2 数据验证

class ListInfo {
    private validateData() {
        if (!ListInfoValidator.validateIcon(this.icon)) {
            throw new Error('Invalid icon resource');
        }
        if (!ListInfoValidator.validateName(this.name)) {
            throw new Error('Invalid name resource');
        }
    }
}

5. 工具方法

5.1 数据转换

class ListInfoConverter {
    // 转换为显示格式
    static toDisplayString(info: ListInfo): string {
        return `${info.name} (${info.icon ? 'Has Icon' : 'No Icon'})`;
    }

    // 转换为存储格式
    static toStorageFormat(info: ListInfo): string {
        return JSON.stringify({
            icon: info.icon,
            name: info.name
        });
    }
}

5.2 数据克隆

class ListInfo {
    clone(): ListInfo {
        return new ListInfo(this.icon, this.name);
    }

    static fromObject(obj: any): ListInfo {
        return new ListInfo(obj.icon, obj.name);
    }
}

6. 性能优化

6.1 数据缓存

class ListInfoCache {
    private static cache: Map<string, ListInfo> = new Map();

    static get(key: string): ListInfo | undefined {
        return this.cache.get(key);
    }

    static set(key: string, info: ListInfo): void {
        this.cache.set(key, info);
    }

    static clear(): void {
        this.cache.clear();
    }
}

6.2 批量操作

class ListInfoBatchProcessor {
    static updateBatch(items: ListInfo[], updates: Partial<ListInfo>): void {
        items.forEach(item => {
            Object.assign(item, updates);
        });
    }
}

7. 最佳实践

7.1 数据管理

  1. 使用响应式数据
  2. 实现数据验证
  3. 提供工具方法
  4. 优化性能表现

7.2 使用建议

  1. 合理设置默认值
  2. 验证数据有效性
  3. 实现数据转换
  4. 优化数据操作

8. 使用示例

8.1 基础用法

// 创建列表项
const item = new ListInfo(
    $r("app.media.icon_example"),
    $r("app.string.name_example")
);

// 更新属性
item.name = $r("app.string.new_name");

8.2 批量处理

// 创建多个列表项
const items = [
    new ListInfo($r("app.media.icon1"), $r("app.string.name1")),
    new ListInfo($r("app.media.icon2"), $r("app.string.name2"))
];

// 批量更新
ListInfoBatchProcessor.updateBatch(items, {
    icon: $r("app.media.new_icon")
});

9. 小结

本篇教程详细介绍了:

  1. 数据模型的基础设计
  2. 响应式机制的实现
  3. 数据验证的方法
  4. 工具方法的开发
  5. 性能优化的策略

下一篇将介绍手势交互系统的实现。


全栈若城
1 声望2 粉丝