Android Launcher3安装应用后,控制应用图标显示位置

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

public void addAndBindAddedWorkspaceApps(final Context context,
                                         final ArrayList<ItemInfo> 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<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
        name, launchIntent, startSearchPageIndex, workspaceScreens);

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

static Pair<Long, int[]> findNextAvailableIconSpace(Context context, String name,
                                                    Intent launchIntent,
                                                    int firstScreenIndex,
                                                    ArrayList<Long> 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<ItemInfo> 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<Long, int[]>(workspaceScreens.get(screen), tmpCoordinates);
            }
        }
    }
    return null;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To add custom functions to the NX menu, you can use the NXOpen.MenuBar class in NXOpen.NET API. Here is an example code that demonstrates how to add a custom function to the NX menu: ```vb Imports System Imports NXOpen Module Module1 Sub Main() ' Get the NX session Dim theSession As Session = Session.GetSession() ' Get the UI work part Dim theUI As UI = theSession.UI Dim lw As ListingWindow = theSession.ListingWindow ' Get the menu bar Dim menuBar As MenuBar = theUI.MenuBar ' Get the File menu Dim fileMenu As Menu = menuBar.GetMenu("File") ' Add a separator to the File menu fileMenu.AddSeparator() ' Add a custom function to the File menu Dim menuItem As MenuItem = fileMenu.AddMenuItem("Custom Function", AddressOf CustomFunction) ' Show a message box when the custom function is clicked Sub CustomFunction(ByVal item As MenuItem) lw.Open() lw.WriteLine("Custom function is clicked!") lw.Close() End Sub ' Start the NX message loop to display the menu theUI.NXMessageBox.Show("Menu Example", NXMessageBox.DialogType.Information, "Click OK to display the menu") theUI.NXMessageBox.GetMessage() ' Remove the custom function from the menu fileMenu.RemoveMenuItem(menuItem) End Sub End Module ``` In the above code, we first obtain the NX session and UI work part. Then, we get the MenuBar object using `theUI.MenuBar`. Next, we retrieve the desired menu (e.g., "File") using `GetMenu()` method. We can add a separator using `AddSeparator()` method and add a custom function using `AddMenuItem()` method, specifying the function to be called when the menu item is clicked. In the example above, the `CustomFunction` is a sub that will be executed when the custom function menu item is clicked. You can customize the behavior of this function to perform your desired actions. After adding the custom function, we start the NX message loop using `theUI.NXMessageBox.Show()` and `theUI.NXMessageBox.GetMessage()` to display the menu. Finally, we remove the custom function from the menu using `RemoveMenuItem()` method. Please note that above code is just an example, and you may need to adjust it based on your specific requirements and menu structure in NX.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值