【问题标题】:How to react to end of dragging in a custom component如何对拖入自定义组件的结束做出反应
【发布时间】:2019-06-07 16:44:47
【问题描述】:

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

使用 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


    【解决方案1】:

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

    protected boolean isStickyDrag() {
        return true;
    }
    

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2021-02-23
    相关资源
    最近更新 更多