安卓的原生launcher偶尔出现添加小部件的时候,点击没有效果,没法拖拽的现象。根据现象,大概猜测是google的锅
分析:点击事件有没有传到小部件页面?传到哪里了?我们从头看
长按桌面弹出了,壁纸,小部件,设置的一个popup,点击小部件才跳转到了添加小部件的页面
这段逻辑在alps\packages\apps\Launcher3\src\com\android\launcher3\views\OptionsPopupView.java中
public static boolean onWidgetsClicked(View view) {
return openWidgets(Launcher.getLauncher(view.getContext()));
}
public static boolean openWidgets(Launcher launcher) {
if (launcher.getPackageManager().isSafeMode()) {
Toast.makeText(launcher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
return false;
} else {
WidgetsFullSheet.show(launcher, true /* animated */);
return true;
}
}
跳转到Y:\10\alps\packages\apps\Launcher3\src\com\android\launcher3\widget\WidgetsFullSheet.java之后,这里并没有想要的代码,于是我们查看WidgetsFullSheet的父类,Y:\10\alps\packages\apps\Launcher3\src\com\android\launcher3\widget\BaseWidgetSheet.java,这里发现了我们想要的代码
@Override
public final void onClick(View v) {
// Let the user know that they have to long press to add a widget
if (mWidgetInstructionToast != null) {
mWidgetInstructionToast.cancel();
}
CharSequence msg = Utilities.wrapForTts(
getContext().getText(R.string.long_press_widget_to_add),
getContext().getString(R.string.long_accessible_way_to_add));
mWidgetInstructionToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
mWidgetInstructionToast.show();
}
@Override
public boolean onLongClick(View v) {
if (!ItemLongClickListener.canStartDrag(mLauncher)) return false;
if (v instanceof WidgetCell) {
return beginDraggingWidget((WidgetCell) v);
}
return true;
}
长按触发拖拽,但是出现问题的时候发现,if (!ItemLongClickListener.canStartDrag(mLauncher)) return false;,直接返回了false,于是再看看这个方法是什么判断,我们来到Y:\10\alps\packages\apps\Launcher3\src\com\android\launcher3\touch\ItemLongClickListener.java
public static boolean canStartDrag(Launcher launcher) {
if (launcher == null) {
return false;
}
// We prevent dragging when we are loading the workspace as it is possible to pick up a view
// that is subsequently removed from the workspace in startBinding().
if (launcher.isWorkspaceLocked()) return false;
// Return early if an item is already being dragged (e.g. when long-pressing two shortcuts)
if (launcher.getDragController().isDragging()) return false;
return true;
}
最终发现,这个地方返回了false , if (launcher.isWorkspaceLocked()) return false; 上面Google备注了返回false的原因,然并卵,他有他的说法,我有我的需求啊!
直接注释掉这句,就完了,反正没啥其他影响。有时候,粗暴一点,就会简单一点!