前言
如果我们需要固定住布局,不给用户拖拽,可以通过修改长按点击监听事件来达到禁止拖拽的目的
二、代码追踪
1、src/com/android/launcher3/touch/ItemLongClickListener.java
在这个类开头注册了两种类型的监听,一个是在桌面拖拽应用,一个是在所有应用里面长按拖拽
/**
* Class to handle long-clicks on workspace items and start drag as a result.
*/
public class ItemLongClickListener {
public static final OnLongClickListener INSTANCE_WORKSPACE =
ItemLongClickListener::onWorkspaceItemLongClick;
public static final OnLongClickListener INSTANCE_ALL_APPS =
ItemLongClickListener::onAllAppsItemLongClick;
接着分析桌面的长按事件
private static boolean onWorkspaceItemLongClick(View v) {
if (v instanceof LauncherAppWidgetHostView) {
TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "Widgets.onLongClick");
} else {
TestLogging.recordEvent(TestProtocol.SEQUENCE_MAIN, "onWorkspaceItemLongClick");
}
Launcher launcher = Launcher.getLauncher(v.getContext());
if (!canStartDrag(launcher)) return false;//请注意这里,这里判断是否允许拖拽,如果不允许拖拽,则用户长按的时候是没有任何反应的,包括长按卸载和快捷方式啥的都没有了。所以不可以在这里修改
if (!launcher.isInState(NORMAL) && !launcher.isInState(OVERVIEW)) return false;
if (!(v.getTag() instanceof ItemInfo)) return false;
launcher.setWaitingForResult(null);
beginDrag(v, launcher, (ItemInfo) v.getTag(), launcher.getDefaultWorkspaceDragOptions());
return true;
}
我们看到有**beginDrag(v, launcher, (ItemInfo) v.getTag(), launcher.getDefaultWorkspaceDragOptions());**这个方法,这个方法也是不能注释掉的,点进去这个方法
public static void beginDrag(View v, Launcher launcher, ItemInfo info,
DragOptions dragOptions) {
if (info.container >= 0) {
Folder folder = Folder.getOpen(launcher);
if (folder != null) {
if (!folder.getIconsInReadingOrder().contains(v)) {
folder.close(true);
} else {
folder.startDrag(v, dragOptions);
return;
}
}
}
CellLayout.CellInfo longClickCellInfo = new CellLayout.CellInfo(v, info);
launcher.getWorkspace().startDrag(longClickCellInfo, dragOptions);
}
重点关注最后一行,发现走到了我们的主桌面workspace,点进去继续看
public void startDrag(CellLayout.CellInfo cellInfo, DragOptions options) {
View child = cellInfo.cell;
mDragInfo = cellInfo;
//child.setVisibility(INVISIBLE);//需要把这一行注释掉,不然长按的时候,图标会消失不见
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);
}
重点关注最后一行,这一行也不能注释掉,点击去到这里
public void beginDragShared(View child, DragSource source, DragOptions options) {
Object dragObject = child.getTag();
if (!(dragObject instanceof ItemInfo)) {
String msg = "Drag started with a view that has no tag set. This "
+ "will cause a crash (issue 11627249) down the line. "
+ "View: " + child + " tag: " + child.getTag();
throw new IllegalStateException(msg);
}
beginDragShared(child, null, source, (ItemInfo) dragObject,
new DragPreviewProvider(child), options);
}
还是最后一行代码,点进去才到重点
/**
* Core functionality for beginning a drag operation for an item that will be dropped within
* the workspace
*/
public DragView beginDragShared(View child, DraggableView draggableView, DragSource source,
ItemInfo dragObject, DragPreviewProvider previewProvider, DragOptions dragOptions) {
float iconScale = 1f;
if (child instanceof BubbleTextView) {
Drawable icon = ((BubbleTextView) child).getIcon();
if (icon instanceof FastBitmapDrawable) {
iconScale = ((FastBitmapDrawable) icon).getAnimatedScale();
}
}
// Clear the pressed state if necessary
child.clearFocus();
child.setPressed(false);
if (child instanceof BubbleTextView) {
BubbleTextView icon = (BubbleTextView) child;
icon.clearPressedBackground();
}
if (draggableView == null && child instanceof DraggableView) {
draggableView = (DraggableView) child;
}
final View contentView = previewProvider.getContentView();
final float scale;
// The draggable drawable follows the touch point around on the screen
final Drawable drawable;
if (contentView == null) {
drawable = previewProvider.createDrawable();
scale = previewProvider.getScaleAndPosition(drawable, mTempXY);
} else {
drawable = null;
scale = previewProvider.getScaleAndPosition(contentView, mTempXY);
}
int halfPadding = previewProvider.previewPadding / 2;
int dragLayerX = mTempXY[0];
int dragLayerY = mTempXY[1];
Point dragVisualizeOffset = null;
Rect dragRect = new Rect();
if (draggableView != null) {
draggableView.getSourceVisualDragBounds(dragRect);
dragLayerY += dragRect.top;
dragVisualizeOffset = new Point(- halfPadding, halfPadding);
}
if (child.getParent() instanceof ShortcutAndWidgetContainer) {
mDragSourceInternal = (ShortcutAndWidgetContainer) child.getParent();
}
if (child instanceof BubbleTextView) {
BubbleTextView btv = (BubbleTextView) child;
if (!dragOptions.isAccessibleDrag) {
dragOptions.preDragCondition = btv.startLongPressAction();
}
if (btv.isDisplaySearchResult()) {
dragOptions.preDragEndScale = (float) mAllAppsIconSize / btv.getIconSize();
}
}
final DragView dv;
//start change that do not to drop the workspace and allApps icon.
/*if (contentView instanceof View) {
if (contentView instanceof LauncherAppWidgetHostView) {
mDragController.addDragListener(new AppWidgetHostViewDragListener(mLauncher));
}
dv = mDragController.startDrag(
contentView,
draggableView,
dragLayerX,
dragLayerY,
source,
dragObject,
dragVisualizeOffset,
dragRect,
scale * iconScale,
scale,
dragOptions);
} else {
dv = mDragController.startDrag(
drawable,
draggableView,
dragLayerX,
dragLayerY,
source,
dragObject,
dragVisualizeOffset,
dragRect,
scale * iconScale,
scale,
dragOptions);
}
return dv;*/
//end change that do not to drop the workspace and allApps icon.
return null;
}
这个方法是拖拽的核心方法,在开始拖拽之前执行,改方法前面都是一些计算,不用看,重点看final DragView dv;后面这个if语句,这里构造一个拖拽对象,我们将该对象构造过程给注释掉,返回一个null,这样子,后续的所有拖拽流程都无法进行了,因为没有拖拽对象了,这样子我们就把禁止拖拽禁掉了,同时长按桌面添加微件的功能还是正常的,不影响其他长按功能。
因为所有应用里面的拖拽最终也是走到了最后一个方法,所以所有应用的拖拽也被禁掉了