做棋牌網(wǎng)站合法嗎網(wǎng)站排名seo
前言
如果我們需要固定住布局,不給用戶拖拽,可以通過(guò)修改長(zhǎng)按點(diǎn)擊監(jiān)聽(tīng)事件來(lái)達(dá)到禁止拖拽的目的
二、代碼追蹤
1、src/com/android/launcher3/touch/ItemLongClickListener.java
在這個(gè)類開(kāi)頭注冊(cè)了兩種類型的監(jiān)聽(tīng),一個(gè)是在桌面拖拽應(yīng)用,一個(gè)是在所有應(yīng)用里面長(zhǎng)按拖拽
/*** 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;
接著分析桌面的長(zhǎng)按事件
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;//請(qǐng)注意這里,這里判斷是否允許拖拽,如果不允許拖拽,則用戶長(zhǎng)按的時(shí)候是沒(méi)有任何反應(yīng)的,包括長(zhǎng)按卸載和快捷方式啥的都沒(méi)有了。所以不可以在這里修改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());**這個(gè)方法,這個(gè)方法也是不能注釋掉的,點(diǎn)進(jìn)去這個(gè)方法
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);}
重點(diǎn)關(guān)注最后一行,發(fā)現(xiàn)走到了我們的主桌面workspace,點(diǎn)進(jìn)去繼續(xù)看
public void startDrag(CellLayout.CellInfo cellInfo, DragOptions options) {View child = cellInfo.cell;mDragInfo = cellInfo;//child.setVisibility(INVISIBLE);//需要把這一行注釋掉,不然長(zhǎng)按的時(shí)候,圖標(biāo)會(huì)消失不見(jiàn)if (options.isAccessibleDrag) {mDragController.addDragListener(new AccessibleDragListenerAdapter(this, WorkspaceAccessibilityHelper::new) {@Overrideprotected void enableAccessibleDrag(boolean enable) {super.enableAccessibleDrag(enable);setEnableForLayout(mLauncher.getHotseat(), enable);}});}beginDragShared(child, this, options);}
重點(diǎn)關(guān)注最后一行,這一行也不能注釋掉,點(diǎn)擊去到這里
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);}
還是最后一行代碼,點(diǎn)進(jìn)去才到重點(diǎn)
/*** 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 necessarychild.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 screenfinal 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;}
這個(gè)方法是拖拽的核心方法,在開(kāi)始拖拽之前執(zhí)行,改方法前面都是一些計(jì)算,不用看,重點(diǎn)看final DragView dv;后面這個(gè)if語(yǔ)句,這里構(gòu)造一個(gè)拖拽對(duì)象,我們將該對(duì)象構(gòu)造過(guò)程給注釋掉,返回一個(gè)null,這樣子,后續(xù)的所有拖拽流程都無(wú)法進(jìn)行了,因?yàn)闆](méi)有拖拽對(duì)象了,這樣子我們就把禁止拖拽禁掉了,同時(shí)長(zhǎng)按桌面添加微件的功能還是正常的,不影響其他長(zhǎng)按功能。
因?yàn)樗袘?yīng)用里面的拖拽最終也是走到了最后一個(gè)方法,所以所有應(yīng)用的拖拽也被禁掉了