查看效果图
-
删除自动补位

-
拖拽自动排位

-
跨页拖拽自动排位

需要做的 todo list
- 删除移位
- 删除图标时后方应用前移
- 拖拽移位
- 页面内拖拽
- 应用前移/后移
- 跨页拖拽
- 拖拽到满屏页面
- 页面内拖拽
删除移位
- 删除应用时后方应用前移
- 修改
Workspace.java中removeItemsByMatcher方法:
java
public void removeItemsByMatcher(final Predicate<ItemInfo> matcher) {
// RemoveItem used to save the removed item
ItemInfo removedItem = null;
for (int i = container.getChildCount() - 1; i >= 0; i--) {
View child = container.getChildAt(i);
ItemInfo info = (ItemInfo) child.getTag();
if (matcher.test(info)) {
layout.removeViewInLayout(child);
if (child instanceof DropTarget) {
mDragController.removeDropTarget((DropTarget) child);
}
// Save the remove item
removedItem = info;
}
...
}
if (removedItem != null) {
// Move children forward, start from the removed empty cell
ReorderManager.moveChildrenForward(workspace, positionProvider, removedItem);
}
}
拖拽移位
页面内拖拽-应用前移/后移
- 在
ReorderAlgorithm.java中修改findReorderSolution方法:
java
fun findReorderSolution(
pixelX: Int, pixelY: Int, minSpanX: Int,
minSpanY: Int, spanX: Int, spanY: Int, direction: IntArray?, dragView: View?, decX: Boolean,
solution: ItemConfiguration
): ItemConfiguration {
...
val success: Boolean = if (dragView is AppWidgetHostView) {
mCellLayout.rearrangementExists(
result[0], result[1], spanX, spanY, direction,
dragView, solution
)
} else {
// Rearrange children in the cell layout
mCellLayout.rearrangeChildren(result[0], result[1], dragView, solution)
}
...
}
- 在
CellLayout.java中添加rearrangeChildren方法:
java
public boolean rearrangeChildren(int cellX, int cellY, View dragView, ItemConfiguration solution) {
// Return early if get invalid cell positions
if (cellX < 0 || cellY < 0) return false;
// Get all views in this cell layout
Comparator<View> comparator = ReorderManager.getComparator();
List<View> views = solution.map.keySet().stream().filter(view -> view instanceof CellView || view instanceof FolderIcon).sorted(comparator).collect(Collectors.toList());
// Get target cell index and dragging cell index
int targetIndex = ReorderManager.findItemIndex(views, new CellPos(cellX, cellY, getWorkspace().getIdForScreen(this)));
int dragIndex = views.indexOf(dragView);
if (targetIndex < 0 || dragIndex < 0) return false;
// If target index > drag index, move views forward, otherwise move backward
boolean isMovingForward = targetIndex > dragIndex;
// Get all views to be moved
int fromIndex = Math.min(targetIndex, dragIndex);
int toIndex = Math.max(targetIndex, dragIndex);
List<View> moveViews = views.subList(fromIndex, toIndex + 1);
// Calc new cell positions for the views to be moved
moveViews.forEach(view -> {
CellAndSpan cellAndSpan = solution.map.get(view);
if (cellAndSpan == null) {
return;
}
int currentIndex = moveViews.indexOf(view);
// Moving forwards, ignore the first dragView
if (isMovingForward && currentIndex == 0) {
return;
}
// Moving backwards, ignore the last targetView
if (!isMovingForward && currentIndex == moveViews.size() - 1) {
return;
}
// Calc the new cell position
CellPos newPos;
if (isMovingForward) {
newPos = ReorderManager.calcNewForwardPosition(view);
} else {
newPos = ReorderManager.calcNewBackwardPosition(view);
}
cellAndSpan.cellX = newPos.cellX;
cellAndSpan.cellY = newPos.cellY;
});
// Set the dragging view's target cell position
if (dragView != null) {
CellAndSpan c = solution.map.get(dragView);
if (c != null) {
c.cellX = cellX;
c.cellY = cellY;
}
}
return true;
}
跨页拖拽
- 在
SpringLoadedDragController.java中修改onAlarm方法:
java
override fun onAlarm(alarm: Alarm) {
mScreen?.let { targetScreen ->
// Snap to the screen that we are hovering over now
val w = mLauncher.workspace
if (!w.isVisible(targetScreen)) {
// If the target screen is full, we need to prepare an empty cell for the dragging view
if (!targetScreen.existsEmptyCell()) {
ReorderManager.prepareEmptyCell(w, targetScreen)
}
w.snapToPage(w.indexOfChild(targetScreen))
}
} ?: run {
mLauncher.dragController.cancelDrag()
}
}