Android13-Launcer3_拖拽时获取当前屏索引

127 阅读1分钟

Launcher3/src/com/android/launcher3/Workspace.java

Workspace

工作区,用来显示APP图标、Hotseat图标、folder文件夹、小部件的区域

拖拽的时候会调用到startDrag()方法

    //声明当前屏索引
    private int mCurAppStartDragScreenIndex;
    
    public void startDrag(CellLayout.CellInfo cellInfo, DragOptions options) {
        View child = cellInfo.cell;

        mDragInfo = cellInfo;
        child.setVisibility(INVISIBLE);

        //获取拖拽时当前屏索引
        mCurAppStartDragScreenIndex = cellInfo.screenId;
        //拖拽时当前屏的APP数目
        CellLayout cellLayout = mWorkspaceScreens.valueAt(mCurAppStartDragScreenIndex);
        if(null != cellLayout.getShortcutsAndWidgets()){
            int startDragScreenAppCount = cellLayout.getShortcutsAndWidgets().getChildCount();
            Toast.makeText(cellInfo.cell.getContext(), 
                    String.format("%d 个", startDragScreenAppCount),Toast.LENGTH_SHORT).show();
        }

        if (options.isAccessibleDrag) {
            mDragController.addDragListener(
                    new AccessibleDragListenerAdapter(this, WorkspaceAccessibilityHelper::new) {
                        @Override
                        protected void enableAccessibleDrag(boolean enable) {
                            super.enableAccessibleDrag(enable);
                            setEnableForLayout(mLauncher.getHotseat(), enable);
                        }
                    });
        }

        beginDragShared(child, this, options);
    }

startDrag()方法会被ItemLongClickListener调用到

Launcher3/src/com/android/launcher3/touch/ItemLongClickListener.java