我需要知道 VerticalPanel 何时改变大小

发布于 2024-09-28 00:04:20 字数 604 浏览 2 评论 0原文

我正在使用 gwt-dnd 在我的 GWT 程序中实现拖放功能。为了使滚动正常工作,我需要

<ScrollPanel>
    <AbsolutePanel>
        <VerticalPanel>
             <!-- lots of draggable widgets -->
        </VerticalPanel>
    </AbsolutePanel>
</ScrollPanel>

手动将 AbsolutePanel 的大小设置为足够大以包含 VerticalPanel。但是,当我将小部件添加到 VerticalPanel 时,VerticalPanel.getOffsetHeight() 报告的大小不会立即更新 - 我想它必须首先由浏览器呈现。所以我无法立即更新 AbsolutePanel 的大小,最终它太小了。啊!

我的权宜之计是设置一个计时器,在 500 毫秒后调整面板大小。届时,getOffsetHeight 通常会返回更新后的值。有什么办法可以立即预览尺寸变化或者其他什么吗?或者,我可以立即强制渲染循环,以便我可以获得新的大小,而无需设置注定容易出错的计时器吗?

I'm using gwt-dnd to implement drag-and-drop functionality in my GWT program. To get scrolling to work right, I need

<ScrollPanel>
    <AbsolutePanel>
        <VerticalPanel>
             <!-- lots of draggable widgets -->
        </VerticalPanel>
    </AbsolutePanel>
</ScrollPanel>

I have to manually set the size of the AbsolutePanel to be large enough to contain the VerticalPanel. When I add widgets to the VerticalPanel, though, the size reported by VerticalPanel.getOffsetHeight() isn't immediately updated - I guess it has to be rendered by the browser first. So I can't immediately update the AbsolutePanel's size, and it ends up being too small. Argh!

My stop-gap solution is to set up a timer to resize the panel 500ms later. By then, getOffsetHeight will usually be returning the updated values. Is there any way to immediately preview the size change, or anything? Or, alternatively, can I force a render loop immediately so that I can get the new size without setting up a timer that's bound to be error-prone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-10-05 00:04:20

这是 DOM 操作的常见问题。 offsetHeight 在添加组件后不久才会更新。我喜欢使用递归计时器来处理这个问题,直到违反先决条件。例如,在您的情况下,有一个添加组件的函数,其定义如下:

public void addComponent(Widget w)
{
 final int verticalPanelHeight = verticalPanel.getOffsetHeight();
 verticalPanel.add(w);
 final Timer t = new Timer(){
  public void run()
  {
   if(verticalPanelHeight != verticalPanel.getOffsetHeight())
    absolutePanel.setHeight(verticalPanel.getOffsetHeight() + 10 + "px");
   else
    this.schedule(100);
  }
 };
 t.schedule(100);
}

This is a common problem with DOM manipulations. The offsetHeight doesn't update until a short time after components are added. I like to handle this using a recursive timer until a pre-condition is violated. E.g. In your case let there be a function which adds components and will be defined as below:

public void addComponent(Widget w)
{
 final int verticalPanelHeight = verticalPanel.getOffsetHeight();
 verticalPanel.add(w);
 final Timer t = new Timer(){
  public void run()
  {
   if(verticalPanelHeight != verticalPanel.getOffsetHeight())
    absolutePanel.setHeight(verticalPanel.getOffsetHeight() + 10 + "px");
   else
    this.schedule(100);
  }
 };
 t.schedule(100);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文