android AppWidget 桌面快捷添加小部件流程

android AppWidget 桌面添加流程
摘要由CSDN通过智能技术生成

Luuncher onCreate stupViews 初始化com.android.launcher3
.Workspace 控件,构造方法中实现setOnTouchListener(new WorkspaceTouchListener(mLauncher, this));
在 WorkspaceTouchListener的onLongPress中

弹出OptionsPopupView.showDefaultOptions(mLauncher, mTouchDownPoint.x, mTouchDownPoint.y);,长按桌面弹出小部件选择,选择小部件

/** Returns WidgetsFullSheet that was opened, or null if nothing was opened. /
@Nullable
public static WidgetsFullSheet openWidgets(Launcher launcher) {
if (launcher.getPackageManager().isSafeMode()) {
// 在安全模式下無法使用小工具
Toast.makeText(launcher, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
return null;
} else {
return WidgetsFullSheet.show(launcher, true /
animated */);
}
}

WidgetsFullSheet 控件中加载小部件到 WidgetsRecyclerView中

/** Gets the {@link WidgetsRecyclerView} which shows all widgets in {@link WidgetsFullSheet}. */
@VisibleForTesting
public static WidgetsRecyclerView getWidgetsView(Launcher launcher) {
return launcher.findViewById(R.id.primary_widgets_list_view);
}
在BaseWidgetSheet的onLongClick中开发创建Widget, beginDraggingWidget方法具体创建,走到startDrag方法

private boolean beginDraggingWidget(WidgetCell v) {
// Get the widget preview as the drag representation
WidgetImageView image = v.getWidgetView();

// If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
// we abort the drag.
if (image.getDrawable() == null && v.getAppWidgetHostViewPreview() == null) {
    return false;
}

PendingItemDragHelper dragHelper = new PendingItemDragHelper(v);
dragHelper.setRemoteViewsPreview(v.getRemoteViewsPreview());
dragHelper.setAppWidgetHostViewPreview(v.getAppWidgetHostViewPreview());

if (image.getDrawable() != null) {
    int[] loc = new int[2];
    getPopupContainer().getLocationInDragLayer(image, loc);

    dragHelper.startDrag(image.getBitmapBounds(), image.getDrawable().getIntrinsicWidth(),
            image.getWidth(), new Point(loc[0], loc[1]), this, new DragOptions());
} else {
    View preview = v.getAppWidgetHostViewPreview();
    int[] loc = new int[2];
    getPopupContainer().getLocationInDragLayer(preview, loc);

    Rect r = new Rect(0, 0, preview.getWidth(), preview.getHeight());
    dragHelper.startDrag(r, preview.getMeasuredWidth(), preview.getMeasuredWidth(),
            new Point(loc[0], loc[1]), this, new DragOptions());
}
close(true);
return true;

}

首先调用AppWidgetHostView.setAppWidget(int appWidgetId, AppWidgetProviderInfo info)
,设置AppWidget的显示View和padding,appWidgetId默认-1

拖动Widget ,DragController.DragListener的实现WidgetHostViewLoader.onDragStart
(),

 /**
     * Start preloading the widget.
     */
    private boolean preloadWidget() 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值