Codenameone 将所有分层窗格组件恢复到其位置并重新绘制

Codenameone 将所有分层窗格组件恢复到其位置并重新绘制,codenameone,Codenameone,这个问题与此有关: 在链接问题中,提出的解决方案是使用假布局。我试过了,但当我试图恢复原始布局时,它会产生副作用 我尝试了一种完全不同的方法,在Android上运行良好我的问题是,为什么以下代码仅在Android上运行良好(它在iPhone上不工作,这似乎忽略了该代码),以及是否有一些小的更改使该代码也在iPhone上运行。 守则: private Map<Component, Dimension> layeredPaneCmps = new HashMap<>();

这个问题与此有关:

在链接问题中,提出的解决方案是使用假布局。我试过了,但当我试图恢复原始布局时,它会产生副作用

我尝试了一种完全不同的方法,在Android上运行良好我的问题是,为什么以下代码仅在Android上运行良好(它在iPhone上不工作,这似乎忽略了该代码),以及是否有一些小的更改使该代码也在iPhone上运行。

守则:

private Map<Component, Dimension> layeredPaneCmps = new HashMap<>();

public void start() {
    if (current != null) {
        current.show();
        layeredPaneRestore(); // it works on Android, but not on iOS
        return;
    }
    [...]
}

public void stop() {
    current = getCurrentForm();
    if (current instanceof Dialog) {
        ((Dialog) current).dispose();
        current = getCurrentForm();
    }
    layeredPaneSave(null);
}

/**
 * Save the position of all layered pane components in a recursive way: just
 * invoke with null as cnt.
 *
 * @param cnt
 */
private void layeredPaneSave(Container cnt) {
    if (cnt == null) {
        layeredPaneCmps.clear();
        cnt = Display.getInstance().getCurrent().getLayeredPane(this.getClass(), true);
    }
    for (int i = 0; i < cnt.getComponentCount(); i++) {
        layeredPaneCmps.put(cnt.getComponentAt(i), new Dimension(cnt.getComponentAt(i).getX(), cnt.getComponentAt(i).getY()));
        if (cnt.getComponentAt(i) instanceof Container) {
            layeredPaneSave((Container) cnt.getComponentAt(i));
        }
    }
}

/**
 * Restores all layered pane components in their position and repaints them.
 */
private void layeredPaneRestore() {
    Container layeredPane = Display.getInstance().getCurrent().getLayeredPane(this.getClass(), true);
    for (Component cmp : layeredPaneCmps.keySet()) {
        cmp.setX(layeredPaneCmps.get(cmp).getWidth());
        cmp.setY(layeredPaneCmps.get(cmp).getHeight());
        cmp.repaint();
    }
    layeredPane.repaint();
}
private Map layeredPaneCmps=new HashMap();
公开作废开始(){
如果(当前!=null){
current.show();
layeredPaneRestore();//它在Android上工作,但在iOS上不工作
返回;
}
[...]
}
公共停车场(){
current=getCurrentForm();
if(当前instanceof对话框){
((对话框)当前).dispose();
current=getCurrentForm();
}
分层窗格保存(空);
}
/**
*以递归方式保存所有分层窗格组件的位置:仅
*以null作为cnt调用。
*
*@param-cnt
*/
专用void layeredPaneSave(容器cnt){
如果(cnt==null){
layeredPaneCmps.clear();
cnt=Display.getInstance().getCurrent().getLayeredPane(this.getClass(),true);
}
对于(int i=0;i
Android和iOS的挂起/恢复行为非常不同,iOS试图最小化重绘和回接,而Android则不断挂起/恢复。我建议登录
stop()
/
start()
方法,以确保它们不会被多次调用

请注意,您不应该调用
repaint()
,它将为您调用。因为repaint()可能会触发布局,所以这可能是个问题。此外,父组件的
repaint()
循环到绘制组件中,因此
layeredPane
就足够了,并且不需要
cmp.repaint()