最近在搞Android的Launcher开发,去掉应用列表后,每次安装应用应用图标都会放在第二页。查看源码后,发现安装应用会走LauncherModel的addAndBindAddedWorkspaceApps方法,添加绑定一个应用图标到workspace中。

public void addAndBindAddedWorkspaceApps(final Context context,                                         final ArrayList workspaceApps) {

而addAndBindAddedWorkspaceApps方法又是通过findNextAvailableIconSpace方法寻找一个空位置存放应用图标。这里如果找不到,就会把页数++。

// Add this icon to the db, creating a new page if necessary.  If there// is only the empty page then we just add items to the first page.// Otherwise, we add them to the next pages.int startSearchPageIndex = workspaceScreens.isEmpty() ? 0 : 1;Pair coords = LauncherModel.findNextAvailableIconSpace(context,        name, launchIntent, startSearchPageIndex, workspaceScreens);

在findNextAvailableIconSpace方法中,我发现总是从我Launcher的第二个页面开始往下寻找空位置。所以我修改了这个方法,让他从第一页开始寻找。这样就完美地解决了安装应用后,应用图标放置位置的问题。

static Pair findNextAvailableIconSpace(Context context, String name,                                                    Intent launchIntent,                                                    int firstScreenIndex,                                                    ArrayList workspaceScreens) {    // Lock on the app so that we don't try and get the items while apps are being added    LauncherAppState app = LauncherAppState.getInstance();    LauncherModel model = app.getModel();    boolean found = false;    synchronized (app) {        if (sWorkerThread.getThreadId() != Process.myTid()) {            // Flush the LauncherModel worker thread, so that if we just did another            // processInstallShortcut, we give it time for its shortcut to get added to the            // database (getItemsInLocalCoordinates reads the database)            model.flushWorkerThread();        }        final ArrayList items = LauncherModel.getItemsInLocalCoordinates(context);        // Try adding to the workspace screens incrementally, starting at the default or center        // screen and alternating between +1, -1, +2, -2, etc. (using ~ ceil(i/2f)*(-1)^(i-1))        //修改控制安装后图标位置        firstScreenIndex = Math.min(firstScreenIndex, workspaceScreens.size());        int count = workspaceScreens.size();        firstScreenIndex = firstScreenIndex >= 1 ? firstScreenIndex - 1 : firstScreenIndex;        for (int screen = firstScreenIndex; screen < count && !found; screen++) {            int[] tmpCoordinates = new int[2];            if (findNextAvailableIconSpaceInScreen(items, tmpCoordinates,                    workspaceScreens.get(screen))) {                // Update the Launcher db                return new Pair(workspaceScreens.get(screen), tmpCoordinates);            }        }    }    return null;}

 

更多相关文章

  1. android自定义相机添加自定义水印
  2. Android(安卓)事件分发机制(最新源码6.0分析)--ViewGrop
  3. android 自定义view中onMeasure()理解
  4. Android欢迎页面闪屏解决方法
  5. Android进程优先级部分整理与理解
  6. Android(安卓)-- Messager与Service
  7. Android(安卓)依赖注入: Dagger 2 实例讲解(一)
  8. Android(安卓)自定义控件
  9. Android(安卓)自动接听来电

随机推荐

  1. Android(安卓)升级Android(安卓)X 所需要
  2. android微信window.location.reload失效
  3. Android中相机,相册,剪切的原生使用
  4. Android源码分析:Java的Media Scanner层(阅
  5. Android好的文章,持续更新
  6. Android下数据库的解析
  7. android 打包 生成apk 出现的问题 javasc
  8. android选择视频文件上传到后台服务器
  9. Android(安卓)开发 设置banner圆角,滑动时
  10. Android(安卓)ListView 实现 GridView 用