如何正确实施修复程序以使向下拖动隐藏的组件起作用?

时间:2019-04-23 17:19:26

标签: codenameone

隐藏一个组件时,其高度被强制为0,这样可以防止在向下拖动Component时进行滚动。基本上,可以将其向下拖动到屏幕边缘,但是不会发生向下滚动。 SO here在前面也提到了这个问题,Shai提供了调查的途径。

是否有解决此问题的建议?

谢谢

以下是重现问题的代码示例(将其复制/粘贴到Hello World项目中,以替换默认的start()方法):

Image dragImage2;
Component dropPlaceholder;

void saveDragImage(Image dragIm) {
    dragImage2 = dragIm;
}

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    /* To seee the issue: press and hold a button about 1 second to initiate the drag and drop, 
    then drag down to the lower edge of the screen: the list doesn't scroll down as normally. 

    When dragging upwards in a list (which must of course alreaday be scrolled down a bit), 
    it works as expected: When the dragged button reaches to top of the screen, the list scrolls down. */

    Form hi = new Form("Hi World", new BorderLayout());
    dropPlaceholder = new Label(" ... "); //used to create an empty space in the drop position
    dropPlaceholder.setDropTarget(true);

    Container list3 = new Container(BoxLayout.y());
    for (int i = 0; i < 30; i++) {

        Button draggableButton = new Button("Button number " + i) {
            @Override
            public void longPointerPress(int x, int y) {
                setDraggable(true);
                setFocusable(true);
                dragImage2 = super.getDragImage();
                setHidden(true);
                Form f = getComponentForm();
                f.pointerPressed(x, y);
                pointerDragged(x - 1, y - 1); //move the dragged element a few pixels to initiate the drag
                pointerDragged(x - 2, y - 2);
            }

            @Override
            protected Image getDragImage() {
                return dragImage2;
            }

            @Override
            protected void dragFinished(int x, int y) {
                setHidden(false);
                if (dropPlaceholder != null)
                    dropPlaceholder.remove(); //remove the placeholder after drag
            }
        };
        draggableButton.setDropTarget(true);

        draggableButton.addDragOverListener((e) -> {
            Component dropTarget = e.getDropTarget(); //may be null (in Component.dragFinishedImpl(int x, int y))
            if (dropTarget != null) {
                Container parent = dropTarget.getParent();
                int index = parent.getComponentIndex(dropTarget);
                dropPlaceholder.remove(); //remove placeholder from previous position
                parent.addComponent(index, dropPlaceholder); //add placeholder at position of dropTarget
            }
            getCurrentForm().revalidate(); //refresh the screen to show the new position of placeholder
        });
        list3.add(draggableButton);
    }
    list3.setScrollableY(true);
    Container cont = hi.getContentPane();
    cont.add(BorderLayout.CENTER, list3);
    hi.show();
}

0 个答案:

没有答案