最近有个项目客户要求实现,桌面卸载应用后,后面的图标向前移动的效果,网上找了也没发现什么的好的办法,只有自己动手写了,前提是我们桌面应用平铺在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);
}
}
```
如果有人实现过这个功能也可以同我分享一下,不胜感激,如果没有思路的同学,也可以看看是否有启发