基于 Launcher3 的 iOS 风格桌面 04 拖拽和移位

查看效果图

  • 删除自动补位

  • 拖拽自动排位

  • 跨页拖拽自动排位

需要做的 todo list

  • 删除移位
    • 删除图标时后方应用前移
  • 拖拽移位
    • 页面内拖拽
      • 应用前移/后移
    • 跨页拖拽
      • 拖拽到满屏页面

删除移位

  • 删除应用时后方应用前移
  • 修改 Workspace.javaremoveItemsByMatcher 方法:
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()
  }
}
相关推荐
2501_915106323 小时前
iOS 反编译防护工具与实战组合 从静态侦察到 IPA 成品加固的工程化路径
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者86 小时前
iOS 26 iPhone 使用记录分析 多工具组合构建全方位设备行为洞察体系
android·ios·小程序·uni-app·cocoa·iphone·webview
zhangphil13 小时前
HARDWARE 属性的Bitmap与普通Bitmap,GPU与RenderThread渲染与处理方式异同比较,Android
android
消失的旧时光-194315 小时前
Flutter 异步编程:Future 与 Stream 深度解析
android·前端·flutter
alexhilton16 小时前
Compose CameraX现已稳定:给Composer的端到端指南
android·kotlin·android jetpack
阿里云云原生18 小时前
移动端性能监控探索:可观测 Android 采集探针架构与实现
android
雨白18 小时前
玩转 Flow 操作符(一):数据转换与过滤
android·kotlin
二流小码农18 小时前
鸿蒙开发:web页面如何适配深色模式
android·ios·harmonyos
消失的旧时光-194320 小时前
TCP 流通信中的 EOFException 与 JSON 半包问题解析
android·json·tcp·数据