HarmonyOS swiper使用LazyForEach加载数据时,界面展示异常

进入swiper界面后,最先展示的一屏布局是对的,左右滑动展示的都是向顶部靠拢的,然后点击界面一个展开菜单的动画,布局又自动回归到正常。

HarmonyOS
2024-12-24 17:38:18
618浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以通过setWindowLayoutFullScreen+margin的方式来实现:

//EntryAbility.ets
//替换掉此方法
onWindowStageCreate(windowStage: window.WindowStage): void {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Index', (err, data) => {
  if (err.code) {
  hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');

let windowClass = windowStage.getMainWindowSync()
AppStorage.setOrCreate('windowClass',windowClass)

let statusHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height;
let bottomHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR).bottomRect.height;
AppStorage.setOrCreate('bottomHeight',px2vp(bottomHeight));
AppStorage.setOrCreate('statusHeight',px2vp(statusHeight));
});
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
//SecondPage.ets
import { DataBean } from '../data/DataBean';
import { Data } from '../data/ResultForReaded';
import { WaterFlowDataSource } from '../data/WaterFlowDataSource';
import { AppRouter } from '../model/AppRouter';
import { CaseDialog } from '../model/CaseDialog';
import { window } from '@kit.ArkUI';

@Entry
@Component
struct SecondPage {
  @Provide('pageInfos') pageInfos: NavPathStack = new NavPathStack()

  onPageShow(): void {
    (AppStorage.get('windowClass') as window.Window).setWindowLayoutFullScreen(true)
  }

  onPageHide(): void {
    (AppStorage.get('windowClass') as window.Window).setWindowLayoutFullScreen(false)
  }

  aboutToAppear() {
    AppRouter.getInstance().register(this.pageInfos);

    let listData = new Array<DataBean>()
    for (let index = 0; index < 10; index++) {
      listData.push(new DataBean(index))
    }
    this.myData.setRefreshData(listData)
  }

  @Builder
  PageMap(name: string) {
    if (name === 'dialog') {
      CaseDialog()
    }
  }

  myData: WaterFlowDataSource = new WaterFlowDataSource()

  build() {
    Swiper() {
      LazyForEach(this.myData, (item: DataBean, index: number) => {
        Column() {
          GridRow({ columns: { sm: 8, md: 8, lg: 8 } }) {
            GridCol({ span: { sm: 4, md: 4, lg: 4 } }) {
              this.item1(item)
            }

            GridCol({ span: { sm: 4, md: 4, lg: 4 } }) {
              this.item2(item)
            }
          }
        }.width("100%").height("100%")
        .justifyContent(FlexAlign.Center)
      }, (item: DataBean) => item.index.toString())
    }
    .margin({ bottom: (AppStorage.get('bottomHeight') as number) })
    .indicator(false)
    .backgroundColor(Color.Red)
    .loop(false)
    .autoPlay(false)
    .width("100%")
    .index(2)
    .onChange(index => {
    })
  }

  @Builder
  item1(item: DataBean) {
    Stack() {
      Column() {
        Text("item:" + item.index)
          .backgroundColor(Color.Yellow)
          .width("100%").height("100%")
          .layoutWeight(1)
      }
    }.alignContent(Alignment.TopStart)
  }

  @Builder
  item2(item: DataBean) {
    Column() {
      Text("item2:" + item.index)
        .backgroundColor(Color.Blue)
        .width("100%").height("100%")
        .layoutWeight(1)
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
分享
微博
QQ
微信
回复
2024-12-24 19:15:09


相关问题
HarmonyOS swiper + LazyForEach使用问题
1158浏览 • 1回复 待解决
HarmonyOS Text加载藏文,显示异常
606浏览 • 1回复 待解决
HarmonyOS 关于Swiper+LazyForEach方案咨询
780浏览 • 1回复 待解决
使用LazyForEach加载列表相关问题
1739浏览 • 1回复 待解决
使用List lazyForeach,reuseId未生效
835浏览 • 1回复 待解决
HarmonyOS LazyForEach 不会懒加载原因
735浏览 • 1回复 待解决
HarmonyOS Imageknife 动图展示异常
1110浏览 • 1回复 待解决