文档管理中心
我的
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
问题处理与运维开发与测试问题应用框架开发UI框架组件使用通过监听List滑动位置实现唤起组件的功能

通过监听List滑动位置实现唤起组件的功能

问题现象

在某些特定的场景下,需要实现滑动列表,唤起组件的功能,来提升人机交互的体验效果。如何实现该功能?

背景知识

List组件作为官方提供的列表组件,与其它滚动组件一样具有位置监听功能,从而能实现滑动到指定位置唤起相关组件的功能。

  • 监听坐标位置的方法:onAreaChange事件:监听由布局变化所导致的组件大小、位置发生变化时的回调。其参数为:
    • oldValue:返回目标元素变化之前的宽高以及目标元素相对父元素和页面左上角的坐标位置。
    • newValue:返回目标元素变化之后的宽高以及目标元素相对父元素和页面左上角的坐标位置。
  • 监听偏移量的方法:
    • onWillScroll事件:回调当前帧将要滚动的偏移量和当前滚动状态和滚动操作来源,其中回调的偏移量为计算得到的将要滚动的偏移量值,并非最终实际滚动偏移。可以通过该回调返回值指定滚动组件将要滚动的偏移。其参数类型为OnWillScrollCallback。
    • onScrollFrameBegin事件:列表开始滑动时触发,事件参数传入即将发生的滑动量,事件处理函数中可根据应用场景计算实际需要的滑动量并作为事件处理函数的返回值返回,列表将按照返回值的实际滑动量进行滑动。
    • onDidScroll事件:滚动组件滑动时触发,返回当前帧滑动的偏移量和当前滑动状态。

解决方案

  • 场景一:onAreaChange事件位置监听实现左滑唤醒弹窗:
    收起
    自动换行
    深色代码主题
    复制
    1. @Entry
    2. @Component
    3. struct ListRefreshLoad {
    4. listScroller: ListScroller = new ListScroller();
    5. @State arr: Array<number> = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    6. @State startOffset: number = 0;
    7. @State currentOffset: number = 0;
    8. @State listWidth: number = 0;
    9. @State allowOffset: number = 0;
    10. @State isShow: boolean = false;
    11. // 获取列表项的总长度
    12. getMaxWidth() {
    13. let widthAll = 0;
    14. for (let index = 0; index < this.arr.length; index++) {
    15. widthAll += 180;
    16. }
    17. widthAll -= this.listWidth;
    18. this.allowOffset = widthAll;
    19. }
    20. // 控制“更多”字体大小,
    21. getScale() {
    22. let scale = (this.currentOffset + this.startOffset - this.allowOffset - 30) / 30;
    23. if (scale > 1) {
    24. scale = 1;
    25. }
    26. return scale;
    27. }
    28. build() {
    29. Column() {
    30. Row() {
    31. List({ scroller: this.listScroller }) {
    32. ForEach(this.arr, (item: number) => {
    33. ListItem() {
    34. Row() {
    35. Text(item.toString());
    36. }
    37. .width(180)
    38. .height(80)
    39. .justifyContent(FlexAlign.Center)
    40. .alignItems(VerticalAlign.Center);
    41. };
    42. });
    43. }
    44. .scrollBar(BarState.Off)
    45. .height(80)
    46. .bindSheet($$this.isShow, this.myBuilder(), {
    47. height: 180
    48. })
    49. .onAreaChange((old, newVal) => {
    50. this.listWidth = Number(newVal.width);
    51. })
    52. .onScrollStart(() => {
    53. this.getMaxWidth();
    54. let offset = this.listScroller.currentOffset();
    55. this.startOffset = offset.xOffset;
    56. this.currentOffset = 0;
    57. this.isShow = false;
    58. })
    59. .onWillScroll((offset: number, state: ScrollState) => {
    60. this.currentOffset += offset;
    61. if (state === 2 && this.allowOffset + 60 < this.currentOffset + this.startOffset) {
    62. if (this.isShow) {
    63. return;
    64. }
    65. this.isShow = true;
    66. }
    67. })
    68. .listDirection(Axis.Horizontal);
    69. Column() {
    70. Text('更多')
    71. .fontSize(15);
    72. }
    73. .scale({ x: 0.7 + 0.3 * this.getScale(), y: 0.7 + 0.3 * this.getScale() })
    74. .justifyContent(FlexAlign.Center)
    75. .width(16)
    76. .height(80)
    77. .position({
    78. x: this.listWidth - 30 * this.getScale(),
    79. y: 0
    80. });
    81. };
    82. }
    83. .justifyContent(FlexAlign.Center)
    84. .width('100%')
    85. .height('100%');
    86. }
    87. @Builder
    88. myBuilder() {
    89. Column() {
    90. Text('弹窗')
    91. .margin({ top: 20 });
    92. }
    93. .width('100%')
    94. .height('100%');
    95. }
    96. }

    实现效果如下:

  • 场景二:偏移量监听实现上滑唤醒按钮下滑隐藏按钮:
    收起
    自动换行
    深色代码主题
    复制
    1. import { window } from '@kit.ArkUI';
    2. @Entry
    3. @Component
    4. struct Index {
    5. @State showTitle: boolean = false;
    6. @State msg: string[] = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg', 'hh', 'ii', 'jj'];
    7. private scrollerForList: Scroller = new Scroller();
    8. scrollGetData: number[] = [];
    9. aboutToAppear(): void {
    10. window.getLastWindow(this.getUIContext().getHostContext(), (err, data) => {
    11. data?.setWindowLayoutFullScreen(true); // 设置沉浸式布局
    12. });
    13. }
    14. build() {
    15. Stack({ alignContent: Alignment.Top }) {
    16. List({ space: 10, scroller: this.scrollerForList }) {
    17. ForEach(this.msg, (item: string) => {
    18. ListItem() {
    19. Text(`ListItem${item}`)
    20. .width('100%')
    21. .height('100%')
    22. .borderRadius(15)
    23. .fontSize(24)
    24. .textAlign(TextAlign.Center)
    25. .backgroundColor('#f1f3f5');
    26. }
    27. .width('90%')
    28. .height(100);
    29. }, (item: string) => item);
    30. }
    31. .alignListItem(ListItemAlign.Center)
    32. .width('100%')
    33. .edgeEffect(EdgeEffect.None)
    34. .onDidScroll((offset: number, State: ScrollState) => {
    35. if (offset < 0 && this.showTitle === true) {
    36. this.showTitle = false; // 下滑隐藏组件
    37. }
    38. if (offset > 0 && this.showTitle === false) {
    39. this.showTitle = true; // 上滑显示组件
    40. }
    41. console.info(`offset:${offset},State:${JSON.stringify(State)}`);
    42. });
    43. // 控制是否展示以下'开始组题'的UI组件
    44. if (this.showTitle) {
    45. Row() {
    46. Button('开始组题')
    47. .backgroundColor('#0A59F7');
    48. }
    49. .justifyContent(FlexAlign.Center)
    50. .width('80%')
    51. .height('8%')
    52. .position({ x: '10%', y: '90%' });
    53. }
    54. }
    55. .width('100%')
    56. .height('100%');
    57. }
    58. }

    手指向上滑动同时内容向上,为上滑;手指向下滑动同时内容向下,为下滑。实现效果如下:

常见FAQ

Q:采用偏移量监听List组件,滑动到边界时唤起组件显示异常?

A:以方案二为例,由于滑动组件默认有回弹模式,滑动到边缘时,回弹的偏移会导致showTitle变化,需要设置edgeEffect属性为None,或者增加滑动到边缘的控制条件,限制showTitle变化。

Q:半模态内嵌套Scroll组件如何实现下拉关闭功能?

A:在半模态组件中,监听滚动事件,如果是向下滑动且滑动距离超过一定阈值,则关闭半模态组件即可。