场景一:如何解决List组件内嵌Slider时,上下滑动List却频繁误触发Slider的点击事件?
场景二:在Slider组件中,如何实现一个条件回弹功能:当用户松手(滑动结束)时,若进度未达标,滑块应自动平滑回滚到0。若达标则正常展示。
示例代码如下:
@Observed
class ListItemData {
id: number;
title: string;
minValue: number;
maxValue: number;
currentValue: number;
constructor(id: number, title: string, min: number, max: number, current: number) {
this.id = id;
this.title = title;
this.minValue = min;
this.maxValue = max;
this.currentValue = current;
}
}
@Entry
@Component
struct SliderPage1 {
@State isListScrolling: boolean = false;
@State sliderValues: ListItemData[] = [
new ListItemData(1, '1', 0, 100, 50),
new ListItemData(2, '2', 0, 100, 50),
new ListItemData(3, '3', 0, 100, 50),
new ListItemData(4, '4', 0, 100, 50),
new ListItemData(5, '5', 0, 100, 50),
new ListItemData(6, '6', 0, 100, 50),
new ListItemData(7, '7', 0, 100, 50),
new ListItemData(8, '8', 0, 100, 50),
];
build() {
List({ space: 20 }) {
ForEach(this.sliderValues, (item: ListItemData) => {
ListItem() {
ListItemComponent({ item: item, isListScrolling: this.isListScrolling });
};
}, (item: ListItemData) => item.id.toString());
}
.width('100%')
.height('100%')
.backgroundColor('#F5F5F5')
.onScrollStart(() => { // 监听滚动开始事件
this.isListScrolling = true;
})
.onScrollStop(() => { // 监听滚动结束事件
this.isListScrolling = false;
});
}
}
@Component
struct ListItemComponent {
@ObjectLink item: ListItemData; // 建立对象级绑定
@Prop isListScrolling: boolean;
build() {
Column() {
Text(this.item.title)
.fontSize(18)
.fontColor(Color.Black)
.margin({ bottom: 10 });
Slider({
value: this.item.currentValue,
min: this.item.minValue,
max: this.item.maxValue,
step: 1,
style: SliderStyle.OutSet
})
.width('90%')
.selectedColor('#007DFF')
.onChange((value: number) => {
this.item.currentValue = value;
})
.sliderInteractionMode(this.isListScrolling ? SliderInteraction.SLIDE_ONLY :
SliderInteraction.SLIDE_AND_CLICK_UP);
Text(`当前值:${this.item.currentValue}`)
.fontSize(14)
.fontColor(Color.Gray);
Text(this.isListScrolling ? '滑动中' : '静止');
}
.width('100%')
.padding(15);
}
} 实现效果如下:

@Entry
@Component
struct SliderPage2 {
threshold: number = 60;
@State currentValue: number = 0;
minValue: number = 0;
maxValue: number = 100;
private slideInterval?: number;
aboutToDisappear() {
if (this.slideInterval) {
clearInterval(this.slideInterval);
}
}
private startResetAnimation() {
// 清除之前的计时器
if (this.slideInterval) {
clearInterval(this.slideInterval);
}
this.slideInterval = setInterval(() => {
if (this.currentValue <= 0) {
clearInterval(this.slideInterval);
this.slideInterval = undefined;
return;
}
// 每次减少10步,模拟滑动回0
this.currentValue = Math.max(0, this.currentValue - 10);
}, 16); // 约60fps,更平滑
}
build() {
Column() {
Slider({
min: this.minValue,
max: this.maxValue,
style: SliderStyle.OutSet,
value: this.currentValue,
})
.onChange((value: number, mode: SliderChangeMode) => {
if (mode === SliderChangeMode.End) {
if (value < this.threshold) {
this.startResetAnimation();
} else {
this.currentValue = value;
}
} else {
// 滑动过程中取消正在进行的回弹动画
if (this.slideInterval) {
clearInterval(this.slideInterval);
this.slideInterval = undefined;
}
this.currentValue = value;
}
});
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center);
}
} 实现效果如下:
