codenameone - 如何对拖入自定义组件的结束做出反应
问题描述
我注意到,不能保证在调用其 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();
解决方案
它应该像这样覆盖 isStickyDrag 方法:
protected boolean isStickyDrag() {
return true;
}
它已经定义了它,在这种情况下应该总是接收指针释放事件。
推荐阅读
- java - 在 C++ 和 Java 中使用 Maps
- ios - 如何使用 SwiftUI 遍历字典数组?
- python - 使用 df.to_parquet 将数据帧作为 parquet 文件保存到 S3 时,是否还可以设置 AWS 元数据值?
- java - 如何使用 java 和 maven 下载、解压缩并将压缩文件中的专有 jar 添加到编译中
- python - 在 python 中进行密码学的最佳方法是什么?
- node.js - 如何为公会中的每个用户添加角色?(不和谐的js)
- wordpress - 如何在我的网站 (wordpress) 的标题菜单中为一个特定项目着色?
- python - 如何在 GCP 上部署超过 100 mb 的压缩文件?
- linux - 如何在 Bash 中检查文件是否具有带有正则表达式的扩展名(任何扩展名)?
- c# - 如何在 ASP.NET Core 3 MVC 中向非反应项目添加单个反应?