鸿蒙Harmony 拖动监听DraggedListener
/**
* @param dragMode 设置拦截拖拽方向 Component.DRAG_HORIZONTAL_VERTICAL
* public static final int DRAG_DOWN = 2;
* public static final int DRAG_HORIZONTAL = 2;
* public static final int DRAG_HORIZONTAL_VERTICAL = 1;
* public static final int DRAG_LEFT = 3;
* public static final int DRAG_RIGHT = 4;
* public static final int DRAG_UP = 1;
* public static final int DRAG_VERTICAL = 3;
* @param listener DraggedListener 拖动监听
*/
public void setDraggedListener(int dragMode, Component.DraggedListener listener)
public interface DraggedListener {
void onDragDown(Component var1, DragInfo var2);
void onDragStart(Component var1, DragInfo var2);
void onDragUpdate(Component var1, DragInfo var2);
void onDragEnd(Component var1, DragInfo var2);
void onDragCancel(Component var1, DragInfo var2);
// 返回值 true 拦截滑动事件
default boolean onDragPreAccept(Component component, int dragDirection) { throw new RuntimeException("Stub!"); }
}
public class DragInfo {
public final Point downPoint = null;
public final Point startPoint = null;
public final Point updatePoint = null;
public final double xOffset = 0.0D;
// x轴方向速度,测试发现目前返回值全部为0
public final double xVelocity = 0.0D;
public final double yOffset = 0.0D;
public final double yVelocity = 0.0D;
public DragInfo(Point downPoint, Point startPoint, Point updatePoint, double xOffset, double yOffset, double xVelocity, double yVelocity) {
throw new RuntimeException("Stub!");
}
}
点击事件
滑动事件
层级事件最上层向下层传递
- 点击事件
01-31 20:48:05.851 16868-16868/com.example.harmonyapp E 00001/app: B onDragDown
01-31 20:48:05.851 16868-16868/com.example.harmonyapp E 00001/app: A onDragDown
01-31 20:48:05.888 16868-16868/com.example.harmonyapp E 00001/app: B onDragCancel
01-31 20:48:05.888 16868-16868/com.example.harmonyapp E 00001/app: A onDragCancel
上层拦截点击事件
component.setClickable(true);
- 滑动事件
A拦截消费
01-31 20:50:03.966 16868-16868/com.example.harmonyapp E 00001/app: B onDragDown
01-31 20:50:03.966 16868-16868/com.example.harmonyapp E 00001/app: A onDragDown
01-31 20:50:04.030 16868-16868/com.example.harmonyapp E 00001/app: B onDragPreAccept
01-31 20:50:04.030 16868-16868/com.example.harmonyapp E 00001/app: B onDragStart
01-31 20:50:04.030 16868-16868/com.example.harmonyapp E 00001/app: B onDragUpdate
01-31 20:50:04.031 16868-16868/com.example.harmonyapp E 00001/app: A onDragCancel
01-31 20:50:04.032 16868-16868/com.example.harmonyapp E 00001/app: B onDragUpdate
01-31 20:50:04.147 16868-16868/com.example.harmonyapp E 00001/app: B onDragUpdate
01-31 20:50:04.150 16868-16868/com.example.harmonyapp E 00001/app: B onDragEnd
A不拦截,B拦截消费
01-31 20:51:14.986 18818-18818/com.example.harmonyapp E 00001/app: B onDragDown
01-31 20:51:14.987 18818-18818/com.example.harmonyapp E 00001/app: A onDragDown
01-31 20:51:15.021 18818-18818/com.example.harmonyapp E 00001/app: B onDragPreAccept
01-31 20:51:15.021 18818-18818/com.example.harmonyapp E 00001/app: A onDragPreAccept
01-31 20:51:15.021 18818-18818/com.example.harmonyapp E 00001/app: A onDragStart
01-31 20:51:15.021 18818-18818/com.example.harmonyapp E 00001/app: A onDragUpdate
01-31 20:51:15.022 18818-18818/com.example.harmonyapp E 00001/app: B onDragCancel
01-31 20:51:15.022 18818-18818/com.example.harmonyapp E 00001/app: A onDragUpdate
01-31 20:51:15.335 18818-18818/com.example.harmonyapp E 00001/app: A onDragUpdate
01-31 20:51:15.348 18818-18818/com.example.harmonyapp E 00001/app: A onDragEnd
效果图
源码地址
https://gitee.com/liukerui/harmony-app-examples.git