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

HarmonyOS
2025-01-09 16:07:46
浏览
收藏 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%")
  }
}
  • 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.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
分享
微博
QQ
微信
回复
2025-01-09 17:29:43


相关问题
Swiper组件设置不跟随手滑动
1404浏览 • 1回复 待解决
HarmonyOS 从屏幕最左侧边滑动
582浏览 • 1回复 待解决
HarmonyOS app跟随手机旋转怎么禁止
637浏览 • 1回复 待解决
HarmonyOS 首页金刚滑动demo
694浏览 • 1回复 待解决
关于sidebar侧边遮挡导航问题
1225浏览 • 1回复 待解决
HarmonyOS 如何监听屏幕滑动pop功能
618浏览 • 1回复 待解决
HarmonyOS 跟随键盘工具如何实现
441浏览 • 1回复 待解决
HarmonyOS 如何实现滑动验证码功能
1245浏览 • 1回复 待解决
HarmonyOS toast提示位置跟随键盘移动
406浏览 • 1回复 待解决
HarmonyOS stack子view无法自由位置
530浏览 • 1回复 待解决
HarmonyOS 无限滑动广告模块
489浏览 • 1回复 待解决