那个牌子的指甲剪好用:Android 深入研究拖放功能Launcher(一)

来源:百度文库 编辑:中财网 时间:2024/04/27 03:20:00
1.首先直观感受什么时候开始拖放?我们长按桌面一个应用图标或者控件的时候拖放就开始了,包括在all app view中长按应用图标,下面就是我截取的拖放开始的代码调用堆栈

java代码:
  1. at com.android.launcher2.DragController.startDrag
  2. at com.android.launcher2.Workspace.startDrag
  3. at com.android.launcher2.Launcher.onLongClick
  4. at android.view.View.performLongClick
  5. at android.widget.TextView.performLongClick
  6. at android.view.View$CheckForLongPress.run
  7. at android.os.Handler.handleCallback
  8. at android.os.Handler.dispatchMessage
  9. at android.os.Looper.loop
        桌面应用图标由Launcher.onLongClick负责监听处理,插入断点debug进入onLongclick函数

java代码:
  1. if (!(v instanceof CellLayout)) {
  2.     v = (View) v.getParent();
  3. }

  4. //获取桌面CellLayout上一个被拖动的对象
  5. CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();

  6. if (mWorkspace.allowLongPress()) {
  7. if (cellInfo.cell == null) {

  8. } else {
  9.     if (!(cellInfo.cell instanceof Folder)) {
  10.     //调用Workspace.startDrag处理拖动
  11.         mWorkspace.startDrag(cellInfo);
  12.     }
  13. }

  14. }

       我上面只写出关键代码,首先是获取被拖动的对象v.getTag(),Tag什么时候被设置进去的了

java代码:
  1. public boolean onInterceptTouchEvent(MotionEvent ev) {

  2. if (action == MotionEvent.ACTION_DOWN) {
  3. boolean found = false;

  4. for (int i = count - 1; i >= 0; i--) {
  5. final View child = getChildAt(i);
  6. if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) {
  7. child.getHitRect(frame);

  8. //判断区域是否在这个子控件的区间,如果有把child信息赋给mCellInfo


  9. if (frame.contains(x, y)) {
  10. final LayoutParams lp = (LayoutParams) child.getLayoutParams();
  11. cellInfo.cell = child;
  12. cellInfo.cellX = lp.cellX;
  13. cellInfo.cellY = lp.cellY;
  14. cellInfo.spanX = lp.cellHSpan;
  15. cellInfo.spanY = lp.cellVSpan;
  16. cellInfo.valid = true;
  17. found = true;
  18. mDirtyTag = false;
  19. break;
  20. }
  21. }

  22. }

  23. mLastDownOnOccupiedCell = found;
  24. if (!found) {
  25. //没有child view 说明没有点击桌面图标项
  26. cellInfo.cell = null;
  27. }

  28. setTag(cellInfo);
  29. }