HarmonyOS 侧边栏跟随手的滑动位置自由滑动的功能

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

对于侧边栏和内容区宽度不缩放实现:侧边栏内容和内容区宽度设置不要设成100%或默认的自适应。

参考:

@Entry
@Component
struct Index {
  @State sideBarOffsetX: number = 0
  @State isShowSidebar: boolean = false;
  @State sideBarWidth: Length = 300
  @StorageProp('currentBreakpoint') curBp: string = 'sm'
  @StorageProp('windowWidth') windowWidth: number = 600;

  build() {
    SideBarContainer(SideBarContainerType.Embed) {
      Column() {
        SideBarLayout()
      }
      .justifyContent(FlexAlign.Center)

      Home()
        .width(this.windowWidth)
        .height("100%")
        .onClick(() => {
          this.isShowSidebar = false
        })
    }
    .height('100%')
    .width('100%')
    .controlButton({
      left: 300,
      top:20
    })
    .autoHide(true)
    .sideBarWidth(this.sideBarWidth)
    .minSideBarWidth(50)
    .maxSideBarWidth(300)
    .sideBarPosition(SideBarPosition.Start)
    .showSideBar(this.isShowSidebar)
    .onChange((value) => {
      this.isShowSidebar = value;
      if (!value) {
        this.sideBarWidth = 300
      }
    })
    .divider({ strokeWidth: '1vp', color: Color.Gray, startMargin: '4vp', endMargin: '4vp' })
    .gesture(PanGesture(new PanGestureOptions({direction: PanDirection.Right}))
      .onActionUpdate(event => {
        this.isShowSidebar = true
        this.sideBarWidth = event.offsetX
      })
      .onActionEnd(event => {
        if (event.offsetX < 150) {
          this.isShowSidebar = false;
        }
        this.sideBarWidth = 300
      })
    )
  }
}


@Component
export struct SideBarLayout {
  scroller: Scroller = new Scroller()

  build() {
    RelativeContainer() {
      Image($rawfile('image.png'))
        .id("avatar")
        .height($r('app.float.height_plus'))
        .width($r('app.float.height_plus'))
        .margin({ left: $r('app.float.bottom_middle'),
          top: $r('app.float.sidebar_title_margin_top')})
        .alignRules({
          top: {anchor:"__container__", align: VerticalAlign.Top},
          left: {anchor: "__container__", align: HorizontalAlign.Start}
        })

      Text("这是昵称")
        .id("nickname")
        .fontSize(24)
        .margin({left: 12})
        .alignRules({
          center: {anchor: "avatar", align: VerticalAlign.Center},
          left: {anchor: "avatar", align: HorizontalAlign.End}
        })

      Scroll(this.scroller) {
        Column() {
          Text("服务中心")
            .id("service_title")
            .fontSize(18)
            .fontWeight(FontWeight.Bold)
            .margin({top: $r('app.float.sidebar_title_margin_top'),
              left: $r("app.float.sidebar_title_margin_left")})
            .height(24)
            .width("100%")

          SideBarGridGroup({dataArray: SERVER_CENTER_DATA})
            .margin({top: $r('app.float.grid_top_margin')})
        }
      }
      .height('auto')
      .scrollable(ScrollDirection.Vertical)
      .friction(0.6)
      .edgeEffect(EdgeEffect.None)
      .alignRules({
        left: {anchor: "__container__", align: HorizontalAlign.Start},
        top: {anchor: "avatar", align: VerticalAlign.Bottom}
      })

      Divider()
        .strokeWidth(0.5)
        .color('#FAFAFA')
        .alignRules({
          bottom: {anchor: 'grid_bottom', align: VerticalAlign.Top},
          left: {anchor: '__container__', align: HorizontalAlign.Start}
        })

      Row() {
        ForEach(SIDEBAR_BOTTOM_DATA, (item: SideBarItemModel) => {
          Column({space: 4}) {
            Image(item.imageSrc)
              .width(26)
              .height(26)
              .objectFit(ImageFit.Contain)

            Text(item.title)
              .fontSize(12)
              .textAlign(TextAlign.Center)
          }
          .justifyContent(FlexAlign.Center)
        })
      }
      .id("grid_bottom")
      .width("100%")
      .height(60)
      .align(Alignment.Center)
      .justifyContent(FlexAlign.SpaceEvenly)
      .alignRules({
        bottom: {anchor: "__container__", align: VerticalAlign.Bottom},
        left: {anchor: "__container__", align: HorizontalAlign.Start}
      })
    }
    // 如下宽度设置成具体值
    .width(320)
    .height("100%")
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
Swiper组件设置不跟随手滑动
785浏览 • 1回复 待解决
HarmonyOS 从屏幕最左侧边滑动
142浏览 • 1回复 待解决
HarmonyOS app跟随手机旋转怎么禁止
161浏览 • 1回复 待解决
HarmonyOS 首页金刚滑动demo
118浏览 • 1回复 待解决
关于sidebar侧边遮挡导航问题
616浏览 • 1回复 待解决
HarmonyOS 如何监听屏幕滑动pop功能
21浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
685浏览 • 1回复 待解决