@Entry
@Component
struct ListRefreshLoad {
@State showList: boolean[] = [false, false];
private arr: number[] = [1, 2, 3, 4];
build() {
Column({ space: 15 }) {
Column() {
Row() {
Text('列表一')
.height('100%')
.fontSize(20)
.textAlign(TextAlign.Start);
Image($r('sys.media.ohos_ic_public_arrow_right'))
.height(25)
.width(25)
.objectFit(ImageFit.Contain)
.rotate({ angle: this.showList[0] ? 90 : 0 });
}
.height(30)
.width('100%')
.onClick(() => {
this.showList[0] = !this.showList[0];
});
if (this.showList[0]) {
List({ space: 5 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('子列表' + item.toString())
.height(40)
.width('90%')
.fontSize(20)
.textAlign(TextAlign.Start);
}
.width('100%')
.padding({ left: 5, right: 5 });
});
}
.backgroundColor('#f1f3f5')
.borderRadius(16)
.transition(TransitionEffect.OPACITY.animation({ duration: 1000, curve: Curve.Ease })); // 使用了OPACITY进行透明度形式的转场
}
Row() {
Text('列表二')
.height('100%')
.fontSize(20)
.textAlign(TextAlign.Start);
Image($r('sys.media.ohos_ic_public_arrow_right'))
.height(25)
.width(25)
.objectFit(ImageFit.Contain)
.rotate({ angle: this.showList[1] ? 90 : 0 });
}
.height(30)
.width('100%')
.onClick(() => {
this.showList[1] = !this.showList[1];
});
}
.width('100%')
.height(this.showList[0] ? 265 : 90)
// 组件高度改变时使用动画进行过渡
.animation({
duration: 1000,
curve: Curve.EaseOut,
playMode: PlayMode.Normal
})
.justifyContent(FlexAlign.SpaceBetween);
}
.padding({ left: 10, right: 10 })
.width('100%')
.height('100%');
}
} 将List组件的转场效果改为逐渐铺展开,与下方组件平移式的转场效果匹配。
@Entry
@Component
struct ListRefreshLoad {
@State showList: boolean[] = [false, false];
private arr: number[] = [1, 2, 3, 4];
@State listHeight: number = 1;
build() {
Column({ space: 15 }) {
Column() {
Row() {
Text('列表一')
.height('100%')
.fontSize(20)
.textAlign(TextAlign.Start);
Image($r('sys.media.ohos_ic_public_arrow_right'))
.height(25)
.width(25)
.objectFit(ImageFit.Contain)
.rotate({ angle: this.showList[0] ? 90 : 0 });
}
.height(30)
.width('100%')
.onClick(() => {
if (!this.showList[0]) {
this.showList[0] = true;
} else {
this.getUIContext().animateTo(
{
duration: 1000, // 动画时长 1 秒
curve: Curve.Linear,
onFinish: () => {
this.showList[0] = false;
}
},
() => {
this.listHeight = 1;
});
}
});
if (this.showList[0]) {
Column() {
List({ space: 5 }) {
ForEach(this.arr, (item: number) => {
ListItem() {
Text('子列表' + item.toString())
.height(40)
.width('90%')
.fontSize(20)
.textAlign(TextAlign.Start);
}
.width('100%')
.padding({ left: 5, right: 5 });
});
};
}
.height(this.listHeight)
.backgroundColor('#f1f3f5')
.borderRadius(16)
.onAppear(() => {
// 对列表展开使用与父组件一样的动画
this.getUIContext().animateTo(
{
duration: 1000, // 动画时长 1 秒
curve: Curve.Linear
},
() => {
this.listHeight = 175; // 改变列表组件的高度
});
});
}
Row() {
Text('列表二')
.height('100%')
.fontSize(20)
.textAlign(TextAlign.Start);
Image($r('sys.media.ohos_ic_public_arrow_right'))
.height(25)
.width(25)
.objectFit(ImageFit.Contain)
.rotate({ angle: this.showList[1] ? 90 : 0 });
}
.height(30)
.width('100%')
.onClick(() => {
this.showList[1] = !this.showList[1];
});
}
.width('100%')
.height(this.showList[0] ? 265 : 90)
.animation({
duration: 1000,
curve: Curve.EaseOut,
playMode: PlayMode.Normal
})
.justifyContent(FlexAlign.Start);
}
.padding({ left: 10, right: 10 })
.width('100%')
.height('100%');
}
}