HarmonyOS 鸿蒙Next自然壁纸实战教程-分类页面
HarmonyOS 鸿蒙Next自然壁纸实战教程-分类页面
07-自然壁纸实战教程-分类页面
前言
分类页面提供到功能比较简洁,提供了基本的搜索、分类功能。我们先上布局结构上来划分模块:
- 顶部搜索框
- 分类标签
- 分类卡片网格
- 跳转到分类列表
顶部搜索框
顶部搜索框正常显示,如果切换到显示搜索结果的时候,便会隐藏。
TextInput({
placeholder: '搜索更多...',
text: $$this.categoryViewModel.text
})
.width('75%')
.height(40)
.backgroundColor('#F5F5F5')
.placeholderColor('#999999')
.borderRadius(20)
.onSubmit(async () => {
this.categoryViewModel.onSubmit()
})
.visibility(this.categoryViewModel.currentTab == -1 ? Visibility.Visible : Visibility.None)
分类标签
分类标签的结构在首页时也编写过一次了
Scroll() {
Row() {
ForEach(LocalData.CategoryData, (item: ICategory, index: number) => {
Text(item.text)
.fontSize(16)
.fontWeight(this.categoryViewModel.currentTab === index ? FontWeight.Bold : FontWeight.Normal)
.fontColor($r('sys.color.comp_background_list_card'))
.backgroundColor(this.categoryViewModel.currentTab === index ? '#2196F3' : '#ffb8b3b3')
.borderRadius(20)
.padding({
left: 16,
right: 16,
top: 8,
bottom: 8
})
.margin({ right: 8 })
.onClick(() => {
if (this.categoryViewModel.currentTab !== index) {
this.categoryViewModel.currentTab = index;
}
})
}, (item: ICategory, index: number) => item.id.toString() + Date.now())
}
.width('auto')
.padding({ left: 8, right: 8 })
}
.scrollable(ScrollDirection.Horizontal)
.scrollBar(BarState.Off)
.width('100%')
.height(50)
纵向的分类列表
这里使用了网格 Grid来实现
// 分类卡片网格
Grid() {
ForEach(LocalData.CategoryData, (item: ICategory) => {
GridItem() {
this.CategoryCardBuilder(item)
}
.onClick(() => {
// 设置当前选中的分类标签
// 查找item在categories数组中的实际索引,而不是使用item.id
const index = LocalData.CategoryData.findIndex(category => category.id === item.id);
this.categoryViewModel.currentTab = index !== -1 ? index : 0;
})
})
}
.columnsTemplate('1fr 1fr')
.columnsGap(16)
.rowsGap(16)
.padding(16)
.scrollBar(BarState.Off)
.layoutWeight(1)
.margin({ bottom: 50 })
.visibility(this.categoryViewModel.currentTab == -1 ? Visibility.Visible : Visibility.None)
当点击不同的分类时,页面会发现变化
搜索结果
这里业务流程主要是:
- 输入框中输入内容,如:超人
- 触发请求,获取最新的数据
- 渲染页面
.onSubmit(async () => {
this.categoryViewModel.onSubmit()
})
import { SensitiveFilter } from "../utils/sensitiveFilter";
import { promptAction } from "@kit.ArkUI";
import { NavigationUtils } from "../utils/NavigationUtils";
import { NavigationConst } from "../const/NavigationConst";
/**
* 分类viewmodel
*/
@ObservedV2
export class CategoryViewModel {
@Trace
currentTab: number = -1
@Trace
// 搜索字段
text: string = ''
/**
* 验证搜索内容
* @returns boolean
*/
async validateSearch(): Promise<boolean> {
if (await SensitiveFilter.hasSensitiveWords(this.text)) {
promptAction.showToast({
message: '搜索内容包含敏感词,请重新输入',
duration: 2000
});
return false;
}
return true;
}
/**
* 进行分类搜索
*/
async onSubmit() {
if (await this.validateSearch()) {
NavigationUtils.getInstance().navigatePush(NavigationConst.Home_View_Wallpaper, this.text)
this.text = ''
}
}
}
如何获取资料
获取资料的途径,可以关注我们 官网的公众号 青蓝逐码 ,输入 项目名称 《自然壁纸》 即可获得以上资料。
为什么需要关注公众号
如果我们的资源,网友连关注公众号的欲望都没有,说明我们的这个资料和资源也没有什么太大价值,那么不要也罢,可以让用户付出一些成本的,才是能证明有真正价值的东西。
关于我们
如果你兴趣想要了解更多的鸿蒙应用开发细节和最新资讯,甚至你想要做出一款属于自己的应用!欢迎在评论区留言或者私信或者看我个人信息,可以加入技术交流群。
更多关于HarmonyOS 鸿蒙Next自然壁纸实战教程-分类页面的实战教程也可以访问 https://www.itying.com/category-93-b0.html
鸿蒙Next自然壁纸分类页面可通过ArkUI框架实现。使用Column布局构建整体结构,顶部添加Search组件实现搜索功能。分类区域采用Grid组件展示壁纸分类,设置columnsTemplate为"1fr 1fr"实现两列布局。每个分类项使用Image配合Text组件,绑定onClick事件处理跳转逻辑。数据源使用@State装饰器管理分类数组,通过ForEach动态渲染Grid项。样式使用HarmonyOS通用属性设置圆角、间距等视觉效果。页面应继承CommonPage实现标准导航栏。
更多关于HarmonyOS 鸿蒙Next自然壁纸实战教程-分类页面的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这是一个很好的HarmonyOS Next自然壁纸分类页面实现教程。我来总结几个关键点:
-
页面结构清晰,分为4个主要模块:搜索框、分类标签、分类卡片网格和跳转功能
-
搜索框实现要点:
- 使用TextInput组件
- 设置圆角、背景色等样式
- 通过visibility控制显示/隐藏
- 分类标签实现:
- 使用Scroll+Row+ForEach实现横向滚动
- 通过currentTab状态管理选中样式
- 点击切换时更新currentTab
- 分类卡片网格:
- 使用Grid+ForEach实现2列网格布局
- 通过visibility控制显示逻辑
- 点击卡片时同步更新分类标签选中状态
- 搜索功能:
- 包含敏感词过滤逻辑
- 验证通过后跳转到搜索结果页
- 使用NavigationUtils管理页面跳转
代码实现规范,使用了ObservedV2和Trace进行状态管理,组件样式设置合理,整体架构清晰。特别是分类标签和卡片的状态同步处理得很好。