HarmonyOS Scroller无法滚动到顶部,底部输入框页面被顶上去

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private list: string[] = []
  private scroller = new Scroller()

  aboutToAppear(): void {
    for (let i = 0; i < 200; i++) {
      this.list.push(`选项${i}`)
    }
  }

  build() {
    Column() {
      Text('我是固定标题')
        .fontColor(Color.White)
        .backgroundColor(Color.Red)

      List({ scroller: this.scroller }) {
        ForEach(this.list, (item: string) => {
          ListItem() {
            Text(item)
              .padding(10)
          }
        })
      }
      .layoutWeight(1)
      .onAppear(() => {
        this.scroller.scrollEdge(Edge.Bottom)
        this.scroller.scrollToIndex(this.list.length - 1)
        this.scroller.scrollToIndex(this.list.length)
      })

      TextInput()
    }
    .height('100%')
    .width('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.

代码如上:

问题1:列表加载完数据后,需要滚动到底部,使用场景是聊天页面,使用 this.scroller.scrollEdge 和 this.scroller.scrollToIndex ,都没有滚动到底部

问题2:底部有个输入框,获取到焦点时,整个页面被顶上去了,怎么做到顶部控件固定,列表正常滚动显示

HarmonyOS
2024-12-18 17:01:21
604浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

问题一:可以使用 initialIndex: this.list.length - 1 。如下:

List({ scroller: this.scroller, initialIndex: this.list.length - 1 }) {
  ForEach(this.list, (item: string) => {
    ListItem() {
      Text(item)
        .padding(10)
    }
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

问题二:可以通过设置windowStage.getMainWindowSync().getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.RESIZE)来解决,具体内容请看:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-expand-safe-area-V5#getkeyboardavoidmode

分享
微博
QQ
微信
回复
2024-12-18 18:39:24


相关问题
Web组件怎么知道滚动到顶部
1362浏览 • 1回复 待解决
HarmonyOS webview输入框遮挡
796浏览 • 1回复 待解决
HarmonyOS 输入框无法对齐
1158浏览 • 1回复 待解决
list组件无法滚动到底部
2330浏览 • 1回复 待解决
HarmonyOS webview里的输入框键盘覆盖
717浏览 • 1回复 待解决
HarmonyOS web中的输入框键盘遮住
739浏览 • 1回复 待解决
HarmonyOS 输入框获取焦点后无法弹出
785浏览 • 1回复 待解决