简体   繁体   中英

How to detect if a View is fully rendered?

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

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();

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
                      }};

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM