使用Scroll作为容器,且监听当前窗口的状态,根据不同状态设置Scroll的滑动效果,实现代码如下:
onWindowStageCreate(windowStage: window.WindowStage): void { let windowClass: window.Window | null = null; console.info('windowClass', windowClass); hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); windowStage.loadContent('pages/scrollDemo', (err) => { try { let windowClass = windowStage.getMainWindowSync(); AppStorage.setOrCreate('winClass',windowClass); } catch (exception) { console.error(`Failed to obtain the main window. Cause code: ${exception.code}, message: ${exception.message}`); } if (err.code) { hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); }); }
@State arr: number[] = []; @State scrollDir: ScrollDirection = ScrollDirection.None; @Styles listCard() { .backgroundColor(Color.White) .height(72) .width('100%') .borderRadius(12); }
aboutToAppear(): void { for (let i = 0; i < 30; i++) { this.arr.push(i); } console.info('Succeeded in enabling the listen-----------------------------'); let winClass11: window.Window = AppStorage.get('winClass') as window.Window; let customerWindowStatusType: WindowStatusType = winClass11.getWindowStatus(); if (customerWindowStatusType == 5) { this.SplitScreenMethod(); } else { this.scrollDir = ScrollDirection.None; } try { winClass11.on('windowStatusChange', (WindowStatusType) => { console.info('Succeeded in enabling the listener for window size changes. Data: ' + JSON.stringify(WindowStatusType)); if (WindowStatusType == 5) { this.SplitScreenMethod(); } else { this.scrollDir = ScrollDirection.None; } }); } catch (exception) { console.error(`Failed to enable the listener for window size changes. Cause code: ${exception.code}, message: ${exception.message}`); } } SplitScreenMethod() { console.info('分屏'); this.scrollDir = ScrollDirection.Vertical; }
build() { Scroll() { Column() { Text('Scroll Area') .width('100%') .height('40%') .backgroundColor('#0080DC') .textAlign(TextAlign.Center); Tabs({ barPosition: BarPosition.Start }) { TabContent() { List({ space: 10 }) { ForEach(this.arr, (item: number) => { ListItem() { Text('item' + item) .fontSize(16); }.listCard(); }, (item: string) => item); }.width('92%').scrollBar(BarState.Off) .edgeEffect(EdgeEffect.Spring) .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST }); }.tabBar('Tab1'); TabContent() { }.tabBar('Tab2'); } .height(this.scrollDir === ScrollDirection.None ? '60%' : '100%') .vertical(false); }.width('100%'); } .edgeEffect(EdgeEffect.Spring) .friction(0.6) .backgroundColor('#DCDCDC') .scrollBar(BarState.Off) .scrollable(this.scrollDir) .width('100%') .height('100%'); }
完整示例参考如下:
import { window } from '@kit.ArkUI'; @Entry @Component struct scrollDemo { @State arr: number[] = []; @State scrollDir: ScrollDirection = ScrollDirection.None; @Styles listCard() { .backgroundColor(Color.White) .height(72) .width('100%') .borderRadius(12); } aboutToAppear(): void { for (let i = 0; i < 30; i++) { this.arr.push(i); } console.info('Succeeded in enabling the listen-----------------------------'); let winClass11: window.Window = AppStorage.get('winClass') as window.Window; let customerWindowStatusType: WindowStatusType = winClass11.getWindowStatus(); if (customerWindowStatusType == 5) { this.SplitScreenMethod(); } else { this.scrollDir = ScrollDirection.None; } try { winClass11.on('windowStatusChange', (WindowStatusType) => { console.info('Succeeded in enabling the listener for window size changes. Data: ' + JSON.stringify(WindowStatusType)); if (WindowStatusType == 5) { this.SplitScreenMethod(); } else { this.scrollDir = ScrollDirection.None; } }); } catch (exception) { console.error(`Failed to enable the listener for window size changes. Cause code: ${exception.code}, message: ${exception.message}`); } } SplitScreenMethod() { console.info('分屏'); this.scrollDir = ScrollDirection.Vertical; } build() { Scroll() { Column() { Text('Scroll Area') .width('100%') .height('40%') .backgroundColor('#0080DC') .textAlign(TextAlign.Center); Tabs({ barPosition: BarPosition.Start }) { TabContent() { List({ space: 10 }) { ForEach(this.arr, (item: number) => { ListItem() { Text('item' + item) .fontSize(16); }.listCard(); }, (item: string) => item); }.width('92%').scrollBar(BarState.Off) .edgeEffect(EdgeEffect.Spring) .nestedScroll({ scrollForward: NestedScrollMode.PARENT_FIRST, scrollBackward: NestedScrollMode.SELF_FIRST }); }.tabBar('Tab1'); TabContent() { }.tabBar('Tab2'); } .height(this.scrollDir === ScrollDirection.None ? '60%' : '100%') .vertical(false); }.width('100%'); } .edgeEffect(EdgeEffect.Spring) .friction(0.6) .backgroundColor('#DCDCDC') .scrollBar(BarState.Off) .scrollable(this.scrollDir) .width('100%') .height('100%'); } }