目录

列表展开动效异常,与正在下移的组件重叠

问题现象

列表展开时,与正在下移的组件存在重叠,部分列表内容被下移的组件遮挡。

背景知识

问题定位

  1. 使用DevEco Testing查看发生异常的页面,发现问题组件为List组件。

  2. 查看该List组件的相关设置,List组件使用了OPACITY进行透明度形式的转场,其父组件使用animation在高度变化时使用动画,达到组件平移的动画效果。
    @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组件使用了OPACITY进行透明度形式的转场,与下方组件平移式的转场效果冲突,造成与正在下移的组件重叠。

修改建议

将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%');
  }
}