GWT StackLayoutPanel: dynamic and automatic height

2019-06-22 09:19发布

问题:

I'm using a StackLayoutPanel to show stacked emails of a conversation a la gmail where the header is the sender and the child the email's body. For the dynamic nature of this, I can only estimate the height of my stack in code. To estimate the body's height I could get the client's window's width then roughly guess how many lines the body occupies. But this is laborious and likely to be wrong.

My question is: is there a way for the StackLayoutPanel, as for GWT API 2.2, to know and so set automatically the height in display of the current shown child (plus its header and the other headers)?

I'm afraid not so I've tried other things that also, however, fail. I've tried:

  1. Put the StackLayoutPanel inside a ScrollPanel:

    • If I don't set in code a stack's size, mysteriously I can only see the first stack's header and nothing more, not its body, nor other headers.
    • If I do set it, then I have the problem of the body's height estimation.
  2. Not inside a ScrollPanel: the stack gets automatically the size of remaining window's space, but with too many headers these overlap and superimpose each other.

Also, it seems the default behaviour for StackLayoutPanel is to show the rest of headers at the very end of the allocated space, not just after the before's header's child. Is it possible to change this?

Help on this would be much appreciated.

//I know a similar question is this but I think I expand on it.

回答1:

You are right, you do expand on the previous question where the answer is a simple: stackLayoutPanel.setHeight((40*numChildren + 200)+"px");

The 200px body size is dependent on the content and enough so that StackLayoutPanel is probably not going to cut it for you.

I think the closet you are going to come with StackLayoutPanel is to use a fixed body width and use a ScrollPanel INSIDE the body (not SLP inside SP). This way all emails can show all content in ScrollPanel.

Since this is a bit of a compromise, the alternative is to just implement your own widget. I would probably use a VerticalPanel as the underlying widget and just hide and display div's (while keeping the headers visible all the time)



回答2:

Maybe it's possible to accomplish this with a custom widget that would wrap a bunch of DisclosurePanels so that only one of them can be shown at a time.



回答3:

If you extend the StackLayoutPanel, and write a resize() method, using the visible widget height, plus some arbitrary number for the height of the panel headers:

public void resize() {
    int panelHeight = super.getVisibleWidget().getOffsetHeight();
    this.setHeight(panelHeight + 200 + "px");
}

Then add a click handler on the panel header that calls this method when clicked, the stack panel resizes dynamically to display the selected panel.