How to detect if a View is fully rendered?

浪尽此生 提交于 2019-12-11 09:49:30

问题


I have a couple of GWT widgets which are displayed in a view.

The view contains content like this:

FlowPanel mainPanel = new FlowPanel();
RootPanel.get().add(mainPanel);
Label label = new Label("test");
mainPanel.add(label);
FlowPanel otherPanel = new FlowPanel();
mainPanel.add(otherPanel);

The mainPanel gets its final height after the view has been fully rendered. I need to get the value of height from the mainPanel after the render process is completed.

Here is what I do so far:

    new Timer() {

        @Override
        public void run() {
            int height = $(mainPanel).height();
            // do something with the mainPanel height
        }
    }.schedule(300);    

Noite: $(mainPanel) is the use of GWTQuery. I set a timer to hope that the view render process is completed when the timer fires. I suppose this is not a very clever solution.

How can I detect if the view is fully rendered in order to get the final height of the mainPanel?

Edit:

I also tried to use:

@Override
protected void onReveal() {
    super.onReveal();
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        @Override
        public void execute() {
            // get mainPanel height is incorrect
        }
    });
}

but it seems that the rendering was not completed so the mainPanel did not have the correct height.

Edit:

It seems that the Scheduler works different on mobile and desktop when using SDM. I created a demo project (https://github.com/confile/GWT-2.7-Scheduler-Test) to show the problem.

I created another question for this issue here: https://stackoverflow.com/questions/27128787/gwt-2-7-scheduler-works-different-on-mobile-and-desktop-in-sdm


回答1:


Gwt itself gives you a hook to do it. It is onLoad(). You can override it.

Example. If you want to know when widgets of a FlowPanel are rendered, you can use

FlowPanel flowPanel = new FlowPanel(){
                      @Override
                      protected void onLoad()
                      {
                        //Do your stuff here
                      }};



回答2:


You should use a Scheduler, not Timer. See GWT: Timer and Scheduler Classes, which explains the distinction.

UPDATE:

Also, what is $(mainPanel)? You should do:

 int height = mainPanel.getOffsetHeight();



回答3:


This was posted in How to detect if a page has fully rendered using jQuery?

$(window).load(function() {
    //everything is loaded
});


来源:https://stackoverflow.com/questions/27108858/how-to-detect-if-a-view-is-fully-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!