名称 | 方案一 | 方案二 |
|---|---|---|
实现逻辑 | 多个底部指示器,底部指示器与标签捆绑,通过标签切换来控制底部指示器的显示与隐藏。 | 一个底部指示器,根据实际交互跟随标签移动。 |
方案优点 | 实现简单。 | 交互体验好。 |
原理图示 |
|
|
效果图示 |
|
|
@Entry
@Component
struct TabSolution1 {
tabArray: Array<number> = [0, 1, 2, 3, 4];
@State focusIndex: number = 0;
index: number = 0;
private controller: TabsController = new TabsController();
indicatorWidth: number = 50;
private scrollerForScroll: Scroller = new Scroller();
// 单独的页签
@Builder
myTabBar(tabName: string, tabItem: number, tabIndex: number) {
Row({ space: 20 }) {
Column() {
Text(tabName + tabItem)
.fontSize(18)
.fontColor(tabIndex === this.focusIndex ? '#0A59F7' : Color.Black)
.id(tabIndex.toString());
// 资源文件需自行替换,可以替换为三角图片
Column()
.width(20)
.height(4)
.backgroundColor('#0A59F7')
.visibility(tabIndex === this.focusIndex ? Visibility.Visible : Visibility.Hidden);
};
}
.justifyContent(FlexAlign.Center)
.constraintSize({ minWidth: 35 })
.width(100)
.height(38)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
}
@Builder
sideComponent(textName: string) {
Row({ space: 20 }) {
// 可以根据需要自行替换
Text(textName).fontSize(18);
}
.justifyContent(FlexAlign.Center)
.constraintSize({ minWidth: 35 })
.height(27)
.backgroundColor('#FFFFFF');
}
build() {
Column() {
Stack({ alignContent: Alignment.TopStart }) {
// List自定义页签
Column() {
Row({ space: 8 }) {
List({ space: 20, initialIndex: 0, scroller: this.scrollerForScroll }) {
ForEach(this.tabArray, (item: number, index: number) => {
ListItem() {
this.myTabBar('页签 ', item, index);
};
}, (item: string) => item);
}
.listDirection(Axis.Horizontal)
.height(30)
.width('80%')
.friction(0.6)
.alignListItem(ListItemAlign.Start)
.scrollBar(BarState.Off)
.width('80%')
.backgroundColor('#FFFFFF');
this.sideComponent('更多');
}
.alignItems(VerticalAlign.Bottom)
.width('100%')
.backgroundColor('#FFFFFF');
}
.alignItems(HorizontalAlign.Start)
.width('100%');
}
.height(40)
.width('100%')
.backgroundColor('#FFFFFF');
Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
ForEach(this.tabArray, (item: number) => {
TabContent() {
Text('这是TabContent ' + item + ' 的内容')
.height(300)
.width('100%')
.textAlign(TextAlign.Center)
.fontSize(30);
}
.backgroundColor('#F1F3F5');
}, (item: string) => item);
}
.width('100%')
.barHeight(0)
.animationDuration(100)
.onChange((index: number) => {
this.focusIndex = index;
this.scrollerForScroll.scrollToIndex(index - 1, true);
});
}
.height('100%');
}
}
名称 | 图片说明 |
|---|---|
偏移量计算 |
|
运行效果 |
|
// 单独的页签
@Builder
myTabBar(tabName: string, tabItem: number, tabIndex: number) {
Row({ space: 20 }) {
Text(tabName + tabItem)
.fontSize(18)
.fontColor(tabIndex === this.focusIndex ? '#0A59F7' : Color.Black)
.id(tabIndex.toString())
.onAreaChange((oldValue: Area, newValue: Area) => {
console.debug(`oldValue:${JSON.stringify(oldValue)}, newValue:${JSON.stringify(newValue)}`);
// 初始位置:底部指示器
if (this.focusIndex === tabIndex && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)) {
if (newValue.position.x !== undefined) {
let positionX = Number.parseFloat(newValue.position.x.toString());
let preMarginLeft = this.indicatorLeftMargin;
this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX;
console.info(`【页签的onAreaChange】preMarginLeft:${preMarginLeft},nowMarginLeft: ${this.indicatorLeftMargin}`);
}
let width = Number.parseFloat(newValue.width.toString());
this.tabWidth = Number.isNaN(width) ? 0 : width;
this.indicatorWidth = this.tabWidth;
}
});
}
.justifyContent(FlexAlign.Center)
.constraintSize({ minWidth: 35 })
.width(100)
.height(30)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.backgroundColor('#FFFFFF');
}
名称 | 图片说明 |
|---|---|
偏移量计算 |
|
运行效果 |
|
Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
ForEach(this.tabArray, (item: number) => {
TabContent() {
Text('这是TabContent ' + item + ' 的内容')
.height(300)
.width('100%')
.textAlign(TextAlign.Center)
.fontSize(30);
}
.backgroundColor('#F1F3F5');
}, (item: string) => item);
}
.onAreaChange((oldValue: Area, newValue: Area) => {
console.debug(`oldValue:${JSON.stringify(oldValue)}, newValue:${JSON.stringify(newValue)}`);
let width = Number.parseFloat(newValue.width.toString());
this.tabsWidth = Number.isNaN(width) ? 0 : width;
})
.width('100%')
.barHeight(0)
.animationDuration(100)
.onChange((index: number) => {
this.focusIndex = index;
this.scrollerForScroll.scrollToIndex(index - 1, true);
})
.onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
console.debug(`index:${index}, event:${JSON.stringify(event)}`);
// 切换动画开始时触发该回调。下划线跟着页面一起滑动
this.focusIndex = targetIndex;
let targetIndexInfo = this.getTextInfo(targetIndex);
this.startAnimateTo(this.animationDuration, targetIndexInfo.left, targetIndexInfo.width);
})
.onGestureSwipe((index: number, event: TabsAnimationEvent) => {
// 在页面跟手滑动过程中,逐帧触发该回调。
let currentIndicatorInfo = this.getCurrentIndicatorInfo(index, event);
this.focusIndex = currentIndicatorInfo.index;
this.indicatorLeftMargin = currentIndicatorInfo.left;
this.tabWidth = currentIndicatorInfo.width;
this.indicatorWidth = currentIndicatorInfo.width;
}); // 获取页签信息,返回距左侧偏移量和页签宽度
private getTextInfo(index: number): Record<string, number> {
try {
const rect = this.getUIContext().getComponentUtils().getRectangleById(index.toString());
return {
'left': this.getUIContext().px2vp(rect.windowOffset.x),
'width': this.getUIContext().px2vp(rect.size.width)
};
} catch (error) {
return { 'left': 0, 'width': 0 };
}
}
private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
let nextIndex = index;
if (index > 0 && event.currentOffset > 0) {
nextIndex--;
} else if (index < this.tabArray.length && event.currentOffset < 0) {
nextIndex++;
}
let indexInfo = this.getTextInfo(index);
let nextIndexInfo = this.getTextInfo(nextIndex);
let swipeRatio = Math.abs(event.currentOffset / this.tabsWidth);
let currentIndex = swipeRatio > 0.5 ? nextIndex : index; // 页面滑动超过一半,tabBar切换到下一页。
let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio;
let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio;
return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth };
}
// 动画效果(使底部指示器与页签同步移动)
private startAnimateTo(duration: number, leftMargin: number, width: number) {
this.getUIContext().animateTo({
duration: duration, // 动画时长
curve: Curve.Linear, // 动画曲线
iterations: 1, // 播放次数
playMode: PlayMode.Normal, // 动画模式
onFinish: () => {
console.info('play end');
}
}, () => {
this.indicatorLeftMargin = leftMargin;
this.tabWidth = width;
this.indicatorWidth = width;
});
} 名称 | 图片说明 |
|---|---|
偏移量计算 |
|
运行效果 |
|
List({ space: 20, initialIndex: 0, scroller: this.scrollerForScroll }) {
ForEach(this.tabArray, (item: number, index: number) => {
ListItem() {
this.myTabBar('页签 ', item, index);
};
}, (item: string) => item);
}
.listDirection(Axis.Horizontal)
.height(30)
.width('80%')
.friction(0.6)
.alignListItem(ListItemAlign.Start)
.scrollBar(BarState.Off)
.width('80%')
.backgroundColor('#FFFFFF')
.onDidScroll((xOffset: number) => {
// 场景三,跟随页签一起移动
this.indicatorLeftMargin -= xOffset;
}); 名称 | 图片说明 |
|---|---|
偏移量计算 |
|
运行效果 |
|
该情况实现的代码同上述 TabBar切换(涉及滑动) 实现代码一致。
@Entry
@Component
struct TabSolution2 {
tabArray: Array<number> = [0, 1, 2, 3, 4];
@State focusIndex: number = 0;
private controller: TabsController = new TabsController();
animationDuration: number = 300;
@State indicatorLeftMargin: number = 0;
@State indicatorWidth: number = 0;
private tabsWidth: number = 0;
private tabWidth: number = 0;
private scrollerForScroll: Scroller = new Scroller();
// 单独的页签
@Builder
myTabBar(tabName: string, tabItem: number, tabIndex: number) {
Row({ space: 20 }) {
Text(tabName + tabItem)
.fontSize(18)
.fontColor(tabIndex === this.focusIndex ? '#0A59F7' : Color.Black)
.id(tabIndex.toString())
.onAreaChange((oldValue: Area, newValue: Area) => {
console.debug(`oldValue:${JSON.stringify(oldValue)}, newValue:${JSON.stringify(newValue)}`);
// 初始位置:底部指示器
if (this.focusIndex === tabIndex && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)) {
if (newValue.position.x !== undefined) {
let positionX = Number.parseFloat(newValue.position.x.toString());
let preMarginLeft = this.indicatorLeftMargin;
this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX;
console.info(`【页签的onAreaChange】preMarginLeft:${preMarginLeft},nowMarginLeft: ${this.indicatorLeftMargin}`);
}
let width = Number.parseFloat(newValue.width.toString());
this.tabWidth = Number.isNaN(width) ? 0 : width;
this.indicatorWidth = this.tabWidth;
}
});
}
.justifyContent(FlexAlign.Center)
.constraintSize({ minWidth: 35 })
.width(100)
.height(30)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.backgroundColor('#FFFFFF');
}
@Builder
sideComponent(textName: string) {
Row({ space: 20 }) {
Text(textName).fontSize(18);
}
.justifyContent(FlexAlign.Center)
.constraintSize({ minWidth: 35 })
.height(30)
.backgroundColor('#FFFFFF');
}
build() {
Column() {
Stack({ alignContent: Alignment.TopStart }) {
// List自定义页签
Column() {
Row({ space: 8 }) {
List({ space: 20, initialIndex: 0, scroller: this.scrollerForScroll }) {
ForEach(this.tabArray, (item: number, index: number) => {
ListItem() {
this.myTabBar('页签 ', item, index);
};
}, (item: string) => item);
}
.listDirection(Axis.Horizontal)
.height(30)
.width('80%')
.friction(0.6)
.alignListItem(ListItemAlign.Start)
.scrollBar(BarState.Off)
.width('80%')
.backgroundColor('#FFFFFF')
.onDidScroll((xOffset: number) => {
// 场景三,跟随页签一起移动
this.indicatorLeftMargin -= xOffset;
});
this.sideComponent('更多');
}
.alignItems(VerticalAlign.Bottom)
.width('100%')
.backgroundColor('#FFFFFF');
}
.alignItems(HorizontalAlign.Start)
.width('100%');
// 资源文件需自行替换,可以替换为三角图片
Column()
.width(50)
.height(4)
.backgroundColor('#0A59F7')
.margin({ left: this.indicatorLeftMargin, top: 30 });
}
.height(40)
.width('100%')
.backgroundColor('#FFFFFF');
Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
ForEach(this.tabArray, (item: number) => {
TabContent() {
Text('这是TabContent ' + item + ' 的内容')
.height(300)
.width('100%')
.textAlign(TextAlign.Center)
.fontSize(30);
}
.backgroundColor('#F1F3F5');
}, (item: string) => item);
}
.onAreaChange((oldValue: Area, newValue: Area) => {
console.debug(`oldValue:${JSON.stringify(oldValue)}, newValue:${JSON.stringify(newValue)}`);
let width = Number.parseFloat(newValue.width.toString());
this.tabsWidth = Number.isNaN(width) ? 0 : width;
})
.width('100%')
.barHeight(0)
.animationDuration(100)
.onChange((index: number) => {
this.focusIndex = index;
this.scrollerForScroll.scrollToIndex(index - 1, true);
})
.onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
console.debug(`index:${index}, event:${JSON.stringify(event)}`);
// 切换动画开始时触发该回调。下划线跟着页面一起滑动
this.focusIndex = targetIndex;
let targetIndexInfo = this.getTextInfo(targetIndex);
this.startAnimateTo(this.animationDuration, targetIndexInfo.left, targetIndexInfo.width);
})
.onGestureSwipe((index: number, event: TabsAnimationEvent) => {
// 在页面跟手滑动过程中,逐帧触发该回调。
let currentIndicatorInfo = this.getCurrentIndicatorInfo(index, event);
this.focusIndex = currentIndicatorInfo.index;
this.indicatorLeftMargin = currentIndicatorInfo.left;
this.tabWidth = currentIndicatorInfo.width;
this.indicatorWidth = currentIndicatorInfo.width;
});
}
.height('100%');
}
// 获取页签信息,返回距左侧偏移量和页签宽度
private getTextInfo(index: number): Record<string, number> {
try {
const rect = this.getUIContext().getComponentUtils().getRectangleById(index.toString());
return {
'left': this.getUIContext().px2vp(rect.windowOffset.x),
'width': this.getUIContext().px2vp(rect.size.width)
};
} catch (error) {
return { 'left': 0, 'width': 0 };
}
}
private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
let nextIndex = index;
if (index > 0 && event.currentOffset > 0) {
nextIndex--;
} else if (index < this.tabArray.length && event.currentOffset < 0) {
nextIndex++;
}
let indexInfo = this.getTextInfo(index);
let nextIndexInfo = this.getTextInfo(nextIndex);
let swipeRatio = Math.abs(event.currentOffset / this.tabsWidth);
let currentIndex = swipeRatio > 0.5 ? nextIndex : index; // 页面滑动超过一半,tabBar切换到下一页。
let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio;
let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio;
return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth };
}
// 动画效果(使底部指示器与页签同步移动)
private startAnimateTo(duration: number, leftMargin: number, width: number) {
this.getUIContext().animateTo({
duration: duration, // 动画时长
curve: Curve.Linear, // 动画曲线
iterations: 1, // 播放次数
playMode: PlayMode.Normal, // 动画模式
onFinish: () => {
console.info('play end');
}
}, () => {
this.indicatorLeftMargin = leftMargin;
this.tabWidth = width;
this.indicatorWidth = width;
});
}
}