文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 如何实现在下面的嵌套滚动中,下面的list可以遮住上面的Scroll Area还要保持嵌套滚动方式不变(API12+)

收起
自动换行
深色代码主题
复制
import { LengthMetrics } from '@kit.ArkUI';


@Entry
@Component
struct NestedScroll {
  @State listPosition: number = 0; // 0代表滚动到List顶部,1代表中间值,2代表滚动到List底部。
  private arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  private scrollerForScroll: Scroller = new Scroller();
  private scrollerForList: Scroller = new Scroller();


  build() {
    Flex() {
      Scroll(this.scrollerForScroll) {
        Column() {
          Text("Scroll Area")
            .width("100%")
            .height(800)
            .backgroundColor(0X330000FF)
            .fontSize(16)
            .textAlign(TextAlign.Center)
            .onClick(() => {
              this.scrollerForList.scrollToIndex(5, false, ScrollAlign.START, { extraOffset: LengthMetrics.vp(5) });
            })


          List({ space: 20, scroller: this.scrollerForList }) {
            ForEach(this.arr, (item: number) => {
              ListItem() {
                Text("ListItem" + item)
                  .width("100%")
                  .height("100%")
                  .borderRadius(15)
                  .fontSize(16)
                  .textAlign(TextAlign.Center)
                  .backgroundColor(Color.White)
              }.width("100%").height(100)
            }, (item: string) => item)
          }
          .width("100%")
          .height("50%")
          .edgeEffect(EdgeEffect.None)
          .friction(0.6)
          .onReachStart(() => {
            this.listPosition = 0;
          })
          .onReachEnd(() => {
            this.listPosition = 2;
          })
          .onScrollFrameBegin((offset: number) => {
            if ((this.listPosition == 0 && offset <= 0) || (this.listPosition == 2 && offset >= 0)) {
              this.scrollerForScroll.scrollBy(0, offset);
              return { offsetRemain: 0 };
            }
            this.listPosition = 1;
            return { offsetRemain: offset };
          })
          // Text("Scroll Area")
          //   .width("100%")
          //   .height("40%")
          //   .backgroundColor(0X330000FF)
          //   .fontSize(16)
          //   .textAlign(TextAlign.Center)
        }
      }
      .width("100%").height("100%")
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding(20)
  }
}
点赞
收藏
回复
1
分享
举报
浏览26 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

参考如下代码:

收起
自动换行
深色代码主题
复制
import { trustedAppService } from '@kit.DeviceSecurityKit'

@Entry
@Component
struct ScrollableText {
  @State private textContent: string = "这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,需要滚动才能看到全部内容。这是一段很长很长的文字,可能会超出固定高度,需要滚动才能看到全部内容,。"
  @State isTextShow: boolean = true;
  @State z_index: number = -1;

  build() {
    Column() {
      Text('lalala').backgroundColor(Color.Gray).width('100%').textAlign(TextAlign.Center).fontSize(20)

      Column() {
        Scroll() {
          Text(this.textContent)
            .fontSize(20)
            .width('100%')
            .textAlign(TextAlign.Start)
          // .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
          //   if (currentRatio < 1) {
          //     this.z_index = 1
          //   } else {
          //     this.z_index = 0
          //   }
          // })
        }
        .onScroll(() => {
          console.log('drag','开始拖拽文字')
          this.z_index = 1
        })
        // .onScrollStop(() => {
        //   console.log('drag','停止拖拽文字')
        //   this.z_index = -1
        // })
        .height(400) // 固定高度
        .zIndex(this.z_index)
        .backgroundColor('#ffeeefb8')

        .position({ x: 0, y: 0 })

        Scroll() {
          Column() {
            Row() {
              Text().height(400).width('100%')
            }
            // .hitTestBehavior(HitTestMode.Transparent)
            // .onTouch(()=>{
            //   this.z_index = 1
            // })
            .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
              if(currentRatio<1){
                console.log('drag','开始拖拽空白占位')
                this.z_index = -1
              }else{
                console.log('drag','停止拖拽空白占位')
                this.z_index = 1
              }

            })
            // .onDragStart(()=>{
            //   console.log('drag','开始拖拽空白占位')
            //   this.z_index = 1
            // })
            // .onDragEnd(()=>{
            //   console.log('drag','停止拖拽空白占位')
            //   this.z_index = -1
            // })

            ForEach('1234567890'.split(''), (item: number) => {
              ListItem() {
                Text(item.toString())
                  .width('100%')
                  .textAlign(TextAlign.Center)
                  .height(100)
                  .border({ width: 1, color: Color.Pink })
                  .backgroundColor('#ffdd1717')
              }
            })
          }
        }
      }
    }
  }
}
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。