如何以编程方式扩展 TopAppBar?

huangapple go评论56阅读模式
英文:

How to expand TopAppBar programmatically?

问题

MediumTopAppBarLargeTopAppBar 有展开和关闭状态。

EnterAlwaysScrollBehavior 在内容被向上拉动时会立即折叠,而在内容被向下拉动时会立即出现。 (来自文档)

如果向下滚动并且页面主体内容发生变化,它仍会保持关闭状态,这是预期的。但是,我想在内容更改或用户按下按钮时触发展开状态,而不需要用户向上滚动。

我需要的是:

一种方法或解决方法来将 TopAppBar 的状态更改为展开。

英文:

MediumTopAppBar and LargeTopAppBar have expanded and closed state.

EnterAlwaysScrollBehavior will immediately collapse when the content is pulled up, and will immediately appear when the content is pulled down. (from the doc)

If scrolled down and the page body content changes it'll still be in the closed state as expected. But I want to trigger an expanded state back when say content changed or a button is pressed without the user scrolling up.

What I need:

A method or workaround to change the TopAppBar state to expand.

答案1

得分: 4

以下是翻译好的部分:

"Assuming you have the LargeTopAppBar scroll behaviour as" 翻译为 "假设您的 LargeTopAppBar 滚动行为为"

"val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())" 翻译为 "val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())"

"and the LargeTopAppBar as" 翻译为 "以及 LargeTopAppBar 如下"

"Scaffold(" 翻译为 "Scaffold("

"topBar = {" 翻译为 "顶部栏 = {"

"LargeTopAppBar(" 翻译为 "LargeTopAppBar("

"title = { Text(text = "title") }," 翻译为 "标题 = { Text(text = "title") },"

"scrollBehavior = scrollBehavior" 翻译为 "scrollBehavior = scrollBehavior"

"floatingActionButton = {" 翻译为 "浮动操作按钮 = {"

"FloatingActionButton(" 翻译为 "FloatingActionButton("

"onClick = {" 翻译为 "点击事件 = {"

"if (scrollBehavior.state.heightOffset < 0.0) {" 翻译为 "如果 (scrollBehavior.state.heightOffset < 0.0) {"

"//expand the LargeTopAppBar" 翻译为 "//展开 LargeTopAppBar"

"scrollBehavior.state.heightOffset = 0f" 翻译为 "scrollBehavior.state.heightOffset = 0f"

"} else {" 翻译为 "} 否则 {"

"//collapse the LargeTopAppBar" 翻译为 "//折叠 LargeTopAppBar"

"scrollBehavior.state.heightOffset = -242f" 翻译为 "scrollBehavior.state.heightOffset = -242f"

"}" 翻译为 "}"

"}) {}" 翻译为 "}) {}"

英文:

Assuming you have the LargeTopAppBar scroll behaviour as

val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())

and the LargeTopAppBar as

Scaffold(
                topBar = {
                    LargeTopAppBar(
                        title = { Text(text = &quot;title&quot;) },
                        scrollBehavior = scrollBehavior
                    )
                },
                floatingActionButton = {
                    FloatingActionButton(
                        onClick = {
                            if (scrollBehavior.state.heightOffset &lt; 0.0) {
                                //expand the LargeTopAppBar
                                scrollBehavior.state.heightOffset = 0f
                            } else {
                                //collapse the LargeTopAppBar
                                scrollBehavior.state.heightOffset = -242f
                            }

                        }
                    ) {
                    }
                },
            ) {}

I tried setting the offset to -1000f, it still performs the same function

huangapple
  • 本文由 发表于 2023年2月8日 22:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定