鸿蒙NEXT开发实战往期必看文章:
一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!
“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到精通)
HarmonyOS NEXT应用开发案例实践总结合(持续更新......)
HarmonyOS NEXT应用开发性能优化实践总结(持续更新......)
介绍
本示例介绍使用List、Text等组件,以及animateTo等接口实现自定义Tab效果
效果预览图
使用说明
1.选中页签,字体放大加粗且后面有背景条,起到强调作用。
2.手势触摸tab内容滑动,背景条跟随手势一起滑动。抬手时,当tab内容滑动距离不足一半时,会自动回弹,而当tab内容滑动距离大于一半时,背景条则会移动到下一个页签。当背景条滑动到一定距离后开始滑动页签条,使背景条始终能够保持在可视范围内。
3.点击页签,可以进行页签切换。
4.滑动页签条,背景条也会随之一起滑动,然后滑动tab内容,页签条会滑动到原处,使背景条处于可视范围内,之后背景条开始跟随手势滑动。
5.动画承接,背景条滑动过程中,触摸屏幕,背景条动画停止,松开手势,背景条继续滑动
下载安装
ohpm install @ohos-cases/custom_animation_tab
快速使用
1.数据准备(以tab单个页面为例)
首先构建一个TabInfo数组,然后向其中传入对应的内容。以base页面为例,首先创建一个@Builder函数,在该函数中填入struct组件,在struct组件的build的函数中编写对应页面。然后,实现对应的tabBar界面,其中传入了一个TabBarItemInterface匿名类,包括了一些必要属性,可以自定义动态属性修改。
// tab数据
tabsInfo: TabInfo[] = [];
this.tabsInfo = [
new TabInfo(CustomAnimationTabConfigure.DEFAULT_BASE_TAB, wrapBuilder(baseBuilder), wrapBuilder(tabBar)),
new TabInfo(CustomAnimationTabConfigure.DEFAULT_UI_TAB, wrapBuilder(uiBuilder), wrapBuilder(tabBar)),
new TabInfo(CustomAnimationTabConfigure.DEFAULT_DYEFFECT_TAB, wrapBuilder(dyEffectBuilder), wrapBuilder(tabBar)),
new TabInfo(CustomAnimationTabConfigure.DEFAULT_THIRTYPARTY_TAB, wrapBuilder(thirdPartyBuilder), wrapBuilder(tabBar)),
new TabInfo(CustomAnimationTabConfigure.DEFAULT_NATIVE_TAB, wrapBuilder(nativeBuilder), wrapBuilder(tabBar)),
new TabInfo(CustomAnimationTabConfigure.DEFAULT_OTHER_TAB, wrapBuilder(otherBuilder), wrapBuilder(tabBar))
]
// baseBuilder页面
import LazyDataSource from './LazyDataSource';
import { SkeletonLayout } from './SkeletonLayout';
@Builder
export function baseBuilder() {
BasePage();
}
@Component
struct BasePage {
@State data: LazyDataSource<string> = new LazyDataSource<string>();
aboutToAppear(): void {
for (let i = 0; i < 100; i++) {
this.data.pushData(`${i}`);
}
}
build() {
Column() {
List() {
LazyForEach(this.data, (data: string) => {
ListItem() {
SkeletonLayout({isMine: false})
}
})
}
.cachedCount(1)
.width("100%")
.height("100%")
}
.width("100%")
.height("100%")
}
}
// tabBar样式
@Builder
function tabBar($$: TabBarItemInterface) {
Text($$.title)
.fontSize($$.curIndex === $$.index ? $r("app.float.custom_animation_tab_list_select_font_size") : $r("app.float.custom_animation_tab_list_unselect_font_size"))
.fontColor($r("app.color.custom_animation_tab_list_font_color"))
.fontWeight($$.curIndex === $$.index ? FontWeight.Bold : FontWeight.Medium)
.textAlign(TextAlign.Center)
.height($r("app.string.custom_animation