我有一个以编程方式动态添加和删除子项的 VBox。 高度设置为 100%,verticalScrollPolicy=auto。
当用户想要向该 VBox 添加另一个子项时,我希望它自动滚动到 VBox 的底部,因为那是添加子项的位置。
我已经尝试了我可以在网上找到的所有解决方案,但无论如何,verticalScrollPosition和maxVerticalScrollPosition都始终等于0。即使我手动滚动到VBox的底部并按下一个警告这些数字的按钮。(即使在之后'validateNow()' 也是如此)。
我唯一能以编程方式更改这些数字的情况是当 VBox 高度以像素为单位设置时,我不希望这样做,因为孩子们都有不同的高度。
请告诉我,可以设置verticalScrollPosition,而无需对高度(以像素为单位)进行硬编码吗? 我在这里错过了一些完全明显的东西吗?
I have a VBox who dynamically adds and removes children programatically. The height is set to 100% and verticalScrollPolicy=auto.
When a user wants to add another child to that Vbox, I want it to autoscroll to the bottom of the VBox since that is where the child is added.
I've tried every solution I could find online, but no matter what, the verticalScrollPosition and maxVerticalScrollPosition are both ALWAYS equal to 0. Even if I manually scroll to the bottom of the VBox and press a button that alerts these numbers.(Even after 'validateNow()' as well).
The only time I can get these numbers to change programmaticaly is when the VBox height is set in pixels, which I don't want since the children all have varying heights.
Plllease tell me that it's possible to set verticalScrollPosition without hard coding the height in pixels? Am I missing something totally obvious here?
发布评论
评论(1)
实际上,您可能没有滚动 VBox; 如果您的 VBox 包含在另一个容器(例如 Canvas 等)中,并且您按照您所说的方式向 VBox 添加项目,则很有可能是 Canvas 进行滚动,而不是 VBox - 在在这种情况下,VBox 确实会为其滚动位置返回 0。
无论如何,你是对的——你必须设置组件的高度; 即使约束布局设置(例如,“bottom='10'”等)也不起作用。 但是,如果您可以设法设置 VBox 的高度,无论是通过将其尺寸以某种方式绑定到另一个控件,还是通过将它们显式设置为子级附加/创建过程的一部分,您应该能够完成您所追求的目标。
下面是我模拟的一个 AIR 应用程序示例,用于说明该示例。 基本上,它只是将随机大小的框添加到 VBox,并在创建每个子项后滚动到 VBox 的底部。
在本例中,VBox 的高度绑定到其包含组件(此处仅是应用程序)的高度。 其他一切都应该是不言自明的。
希望有帮助! 如果您有任何疑问,请回帖。
You might not be scrolling the VBox, actually; there's a good chance that if your VBox is contained by another container, like a Canvas or the like, and you're adding items to the VBox as you say you are, it's the Canvas that's doing the scrolling, not the VBox -- in which case the VBox would indeed return 0 for its scroll position.
One way or another, you're right -- you have to set the component's height; even constraint-layout settings (e.g., "bottom='10'", etc.) won't work. But if you can manage to set the height of the VBox, either by binding its dimensions somehow to another control or by setting them explicitly as part of a child's appending/creation process, you should be able to accomplish what you're after.
Here's an example of an AIR app I've mocked up to illustrate the example. Basically it just adds randomly sized boxes to a VBox, and scrolls to the bottom of the VBox after each child gets created.
In this case, the height of the VBox is being bound to the height of its containing component (here, just the Application). Everything else should be pretty self-explanatory.
Hope that helps! Post back if you have any questions.