Android launcher 卸載應用後,後面的圖標向前移動

最近有個項目客戶要求實現,桌面卸載應用後,後面的圖標向前移動的效果,網上找了也沒發現什麼的好的辦法,只有自己動手寫了,前提是我們桌面應用平鋪在workspace上了
當我們把應用圖標放在卸載處的時候,就會調用secondaryDropTarget 中performDropAction方法,在這裏我們就可以標記這個應用,知道這個用的位置,這樣我們就可以知道卸載的這個應用的信息了
mLauncher.unInstallRemoveItem(view, info,true);

public void unInstallRemoveItem(View view, ItemInfo info,boolean unInstall){
		mUnInstall = unInstall;
		removeItemInfo(info);
	}
public void removeItemInfo(final ItemInfo itemInfo){
	 	// first step get iteminfo X,Y
		container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
		unInstall_screenId = getCurrentWorkspaceScreen();
		Log.e("zyl","removeItemInfo unInstall_screenId ="+unInstall_screenId);
		removeX = itemInfo.cellX;
		removeY = itemInfo.cellY;
		removeSpanX = itemInfo.spanX;
		removeSpanY = itemInfo.spanY;
		screenId = itemInfo.screenId;
	 }

知道這個信息後,我們就要看卸載流程了,當我們卸載應用後,會回調launcher的bindAppInfosRemoved方法
那就在這個方法裏面實現吧,一開始我想的是讀取數據庫然後再更改數據庫裏面的位置座標,但是應用比較多的時候就會出現問題,所以在此修改用一下方式實現

    @Override
    public void bindAppInfosRemoved(final ArrayList<AppInfo> appInfos) {
        mAppsView.getAppsStore().removeApps(appInfos);
		if(removeX != -1 ||removeY !=-1){
			loadWorkspaceEntries(screenId); 
		}
    }
     protected void loadWorkspaceEntries(long screen) {
        ArrayList<ItemInfo> entries = new ArrayList<>();	        
		CellLayout targetLayout = mWorkspace.getScreenWithId(screen);
		if(targetLayout != null){
		ShortcutAndWidgetContainer currContainer = targetLayout.getShortcutsAndWidgets();
		int count = currContainer.getChildCount();		 
		for (int i = 0; i < count; i++) {	            
			View child = currContainer.getChildAt(i);				
			ItemInfo entry = (ItemInfo) child.getTag();
			if(entry != null){
				if(entry.spanX >1 ||entry.spanY >1){
					isNeedMove = false;
					break;
				}
				if(entry.cellY >removeY ||(entry.cellY ==removeY && entry.cellX >removeX )){
					if(removeX== 3){
						if(entry.cellY > removeY){

							if(entry.cellX == 0){
								entry.cellY = entry.cellY -1;
								entry.cellX =3;
							}else{
								entry.cellX = entry.cellX -1;
							}
						}
					}else if(entry.cellY == removeY && entry.cellX > removeX ){
						entry.cellX = entry.cellX -1;
					}else if(entry.cellY >removeY){
						if(entry.cellX == 0){
							entry.cellY = entry.cellY -1;
							entry.cellX =3;
						}else{
							entry.cellX = entry.cellX -1;
						}
					}
				}
				entries.add(entry);
				isNeedMove = true; 
			}
		}
			if(isNeedMove){
				this.getModelWriter().updatasItemsInDatabase(entries, container, screenId);
				mModel.forceReload(); 
			}
		}
	 public void updatasItemsInDatabase(final ArrayList<ItemInfo> items, long container, long screen) {
        ArrayList<ContentValues> contentValues = new ArrayList<>();
        int count = items.size();

        for (int i = 0; i < count; i++) {
            ItemInfo item = items.get(i);
            updateItemInfoProps(item, container, screen, item.cellX, item.cellY);

            final ContentValues values = new ContentValues();
            values.put(Favorites.CONTAINER, item.container);
            values.put(Favorites.CELLX, item.cellX);
            values.put(Favorites.CELLY, item.cellY);
            values.put(Favorites.RANK, item.rank);
            values.put(Favorites.SCREEN, item.screenId);

            contentValues.add(values);
        }
        mWorkerExecutor.execute(new UpdateItemsRunnable(items, contentValues));
    }

這樣還是會有問題,有時桌面圖標還在但是點擊不了,我的解決辦法是在結束綁定的時候

 public void finishBindingItems() {
 		.....
		if(mUnInstall){
			//Log.e("zyl","finishBindingItems mUnInstall ="+mUnInstall);
			mUnInstall = false;
			mWorkspace.snapToPage(unInstall_screenId);
		}
    }
    ```
    如果有人實現過這個功能也可以同我分享一下,不勝感激,如果沒有思路的同學,也可以看看是否有啓發
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章