半模态转场关闭弹窗重新打开时无需重新加载

半模态转场方式,弹出的页面,加载之后,再将当前页面下拉关闭,sheet走了Dismiss动作。 再打开此页面,是重新加载的过程;伙伴希望能保留当前页面,不是dismiss,而是隐藏。

HarmonyOS
2024-05-26 15:38:17
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

使用的核心API

  •  LazyForEach
  •  bindSheet

核心代码解释

@Entry 
@Component 
struct Page_5067 { 
  @State isShow: boolean = false 
  @State isShow2: boolean = false 
  @State sheetHeight: SheetSize | Length = SheetSize.MEDIUM; 
  @State showDragBar: boolean = true; 
  private data: MyDataSource = new MyDataSource(); 
  listScroller: ListScroller = new ListScroller() 
  private scrollOffset: number = 0 
   
  aboutToAppear() { 
    for (let i = 0; i < 10; i++) { 
      this.data.pushData(`hello${i}`); 
    } 
  } 
 
  @Builder 
  myBuilder() { 
    Column() { 
      List({ scroller: this.listScroller }) { 
        LazyForEach(this.data, (item: string, index: number) => { 
          ListItem() { 
            Row() { 
              Text(item) 
                .width('100%') 
                .height('70%') 
            } 
          } 
        }, (item: string) => item) 
      } 
    } 
    .width('100%') 
    .height('80%') 
  } 
 
  build() { 
    Column() { 
      Image($r('app.media.startIcon')).height(this.isShow ? 300 : 600) 
        .animation({ duration: 300 }) 
      Button("transition modal 1") 
        .onClick(() => { 
          this.isShow = !this.isShow 
        }) 
        .fontSize(20) 
        .margin(10) 
        .bindSheet($$this.isShow, this.myBuilder(), { 
          height: this.sheetHeight, 
          dragBar: this.showDragBar, 
          backgroundColor: Color.Pink, 
          onAppear: () => { 
            if (this.scrollOffset) { 
              this.listScroller.scrollTo({ xOffset: 0, yOffset: this.scrollOffset, animation: false }) 
            } 
            console.log("BindSheet onAppear. ") 
          }, 
          onDisappear: () => { 
            this.scrollOffset = this.listScroller.currentOffset().yOffset 
            console.log("BindSheet onDisappear.") 
          } 
        }) 
    } 
    .justifyContent(FlexAlign.Center) 
    .width('100%') 
    .height('100%') 
  } 
} 
 
// Basic implementation of IDataSource to handle data listener 
class BasicDataSource implements IDataSource { 
  private listeners: DataChangeListener[] = []; 
  private originDataArray: string[] = []; 
 
  public totalCount(): number { 
    return 0; 
  } 
 
  public getData(index: number): string { 
    return this.originDataArray[index]; 
  } 
 
  // 该方法为框架侧调用,为LazyForEach组件向其数据源处添加listener监听 
  registerDataChangeListener(listener: DataChangeListener): void { 
    if (this.listeners.indexOf(listener) < 0) { 
      console.info('add listener'); 
      this.listeners.push(listener); 
    } 
  } 
 
  // 该方法为框架侧调用,为对应的LazyForEach组件在数据源处去除listener监听 
  unregisterDataChangeListener(listener: DataChangeListener): void { 
    const pos = this.listeners.indexOf(listener); 
    if (pos >= 0) { 
      console.info('remove listener'); 
      this.listeners.splice(pos, 1); 
    } 
  } 
 
  // 通知LazyForEach组件需要重载所有子组件 
  notifyDataReload(): void { 
    this.listeners.forEach(listener => { 
      listener.onDataReloaded(); 
    }) 
  } 
 
  // 通知LazyForEach组件需要在index对应索引处添加子组件 
  notifyDataAdd(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataAdd(index); 
    }) 
  } 
 
  // 通知LazyForEach组件在index对应索引处数据有变化,需要重建该子组件 
  notifyDataChange(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataChange(index); 
    }) 
  } 
 
  // 通知LazyForEach组件需要在index对应索引处删除该子组件 
  notifyDataDelete(index: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataDelete(index); 
    }) 
  } 
 
  // 通知LazyForEach组件将from索引和to索引处的子组件进行交换 
  notifyDataMove(from: number, to: number): void { 
    this.listeners.forEach(listener => { 
      listener.onDataMove(from, to); 
    }) 
  } 
} 
 
 
class MyDataSource extends BasicDataSource { 
  private dataArray: string[] = []; 
 
  public totalCount(): number { 
    return this.dataArray.length; 
  } 
 
  public getData(index: number): string { 
    return this.dataArray[index]; 
  } 
 
  public addData(index: number, data: string): void { 
    this.dataArray.splice(index, 0, data); 
    this.notifyDataAdd(index); 
  } 
 
  public pushData(data: string): void { 
    this.dataArray.push(data); 
    this.notifyDataAdd(this.dataArray.length - 1); 
  } 
}
分享
微博
QQ
微信
回复
2024-05-27 20:40:47
相关问题
模态转场如何控制固定高度
1814浏览 • 1回复 待解决
基于bindSheet的模态弹窗
817浏览 • 1回复 待解决
如何固定模态转场的高度
353浏览 • 1回复 待解决
模态转场来实现弹框样式的页面
755浏览 • 1回复 待解决
应用怎么实现模态效果
2156浏览 • 1回复 待解决
如何操作canvas重新绘制
934浏览 • 1回复 待解决
弹窗打开关闭动画是否支持自定义
2170浏览 • 1回复 待解决