基于 Launcher3 的 iOS 风格桌面 04 拖拽和移位
查看效果图
-
删除自动补位

-
拖拽自动排位

-
跨页拖拽自动排位

需要做的 todo list
- 删除移位
- 删除图标时后方应用前移
- 拖拽移位
- 页面内拖拽
- 应用前移/后移
- 跨页拖拽
- 拖拽到满屏页面
- 页面内拖拽
删除移位
- 删除应用时后方应用前移
- 修改
Workspace.java中removeItemsByMatcher方法:
public void removeItemsByMatcher(final Predicate<ItemInfo> matcher) {// RemoveItem used to save the removed itemItemInfo 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 itemremovedItem = info;}...}if (removedItem != null) {// Move children forward, start from the removed empty cellReorderManager.moveChildrenForward(workspace, positionProvider, removedItem);}
}
拖拽移位
页面内拖拽-应用前移/后移
- 在
ReorderAlgorithm.java中修改findReorderSolution方法:
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 layoutmCellLayout.rearrangeChildren(result[0], result[1], dragView, solution)}...
}
- 在
CellLayout.java中添加rearrangeChildren方法:
public boolean rearrangeChildren(int cellX, int cellY, View dragView, ItemConfiguration solution) {// Return early if get invalid cell positionsif (cellX < 0 || cellY < 0) return false;// Get all views in this cell layoutComparator<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 indexint 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 backwardboolean isMovingForward = targetIndex > dragIndex;// Get all views to be movedint 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 movedmoveViews.forEach(view -> {CellAndSpan cellAndSpan = solution.map.get(view);if (cellAndSpan == null) {return;}int currentIndex = moveViews.indexOf(view);// Moving forwards, ignore the first dragViewif (isMovingForward && currentIndex == 0) {return;}// Moving backwards, ignore the last targetViewif (!isMovingForward && currentIndex == moveViews.size() - 1) {return;}// Calc the new cell positionCellPos 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 positionif (dragView != null) {CellAndSpan c = solution.map.get(dragView);if (c != null) {c.cellX = cellX;c.cellY = cellY;}}return true;
}
跨页拖拽
- 在
SpringLoadedDragController.java中修改onAlarm方法:
override fun onAlarm(alarm: Alarm) {mScreen?.let { targetScreen ->// Snap to the screen that we are hovering over nowval w = mLauncher.workspaceif (!w.isVisible(targetScreen)) {// If the target screen is full, we need to prepare an empty cell for the dragging viewif (!targetScreen.existsEmptyCell()) {ReorderManager.prepareEmptyCell(w, targetScreen)}w.snapToPage(w.indexOfChild(targetScreen))}} ?: run {mLauncher.dragController.cancelDrag()}
}
