首页 > 解决方案 > 如何对拖入自定义组件的结束做出反应

问题描述

我注意到,不能保证在调用其 pointerPressed 方法之后调用组件的 pointerReleased 方法。

使用 Conponent 派生 - 一个处理指针事件的派生 - 在所有情况下获得指针释放事件通知的推荐方法是什么?

假设我的自定义组件应该是绿色的,点击时会变红。当不再按下指针时,我如何保证它再次变为绿色?


新示例 - 使用 isStickyDrag() 返回 true 的组件派生 - 记录指针事件 - 当组件被按下然后垂直拖动时,周围的 Container 实例滚动 - 然后没有指针释放:

    Form hi = new Form("Hi World", BoxLayout.y());
    hi.setScrollableY(true);
    Component component = new Component() {
        @Override
        protected Dimension calcPreferredSize() {
            return new Dimension(120, 120);
        }
        @Override
        public void paint(Graphics aGraphics) {
            aGraphics.setColor(0x000000);
            aGraphics.drawRoundRect(getX(), getY(), getWidth() - 1, getHeight() - 1, 20, 20);
        }
        @Override
        protected boolean isStickyDrag() {
            return true;
        }
        protected int getDragRegionStatus(int x, int y) {
            return Component.DRAG_REGION_IMMEDIATELY_DRAG_XY;
        };
        @Override
        public void pointerPressed(int x, int y) {
            super.pointerPressed(x, y);
            Log.p("pointerPressed(" + x + ", " + y + ")");
        }
        @Override
        public void pointerReleased(int x, int y) {
            super.pointerReleased(x, y);
            Log.p("pointerReleased(" + x + ", " + y + ")");
        }
    };
    hi.add(component);
    hi.show();

标签: codenameone

解决方案


它应该像这样覆盖 isStickyDrag 方法:

protected boolean isStickyDrag() {
    return true;
}

它已经定义了它,在这种情况下应该总是接收指针释放事件。


推荐阅读