基于 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_9371892310 小时前
2025 优化版神马影视 8.8 源码系统|零基础部署
android·源码·开源软件·源代码管理·机顶盒
モンキー・D・小菜鸡儿12 小时前
Android Jetpack Compose 基础控件介绍
android·kotlin·android jetpack·compose
无风之翼12 小时前
android15 休眠唤醒过程中有时候屏幕显示时间一闪而过
android·锁屏
方白羽14 小时前
Android全局悬浮拖拽视图
android·app·客户端
Jerry15 小时前
Compose 高级状态和附带效应
android
2501_9160074717 小时前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
LFly_ice17 小时前
Nest-管道
android·java·数据库
ab_dg_dp19 小时前
android bugreport 模块源码分析
android
2501_9151063219 小时前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
繁星星繁20 小时前
【Mysql】数据库基础
android·数据库·mysql