【问题标题】:update widget with multiple layouts使用多个布局更新小部件
【发布时间】:2015-09-01 09:08:37
【问题描述】:

我有一个应用程序需要根据大小更改小部件的不同布局,例如,如果小部件是 4x2 单元格,则设置 widget_layout,esle 设置 widget_layout1。

对于这个更改,我使用@Gogu 的method

    @Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {

    Log.d("widget", "Changed dimensions");

    // See the dimensions and
    Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);

    // Get min width and height.
    int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
    int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);

    // Obtain appropriate widget and update it.
    appWidgetManager.updateAppWidget(appWidgetId, getRemoteViews(context, minWidth, minHeight));

    super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
}

/**
 * Determine appropriate view based on width provided.
 *
 * @param minWidth
 * @param minHeight
 * @return
 */
private RemoteViews getRemoteViews(Context context, int minWidth, int minHeight) {

    // First find out rows and columns based on width provided.
    int rows = getCellsForSize(minHeight);
    int columns = getCellsForSize(minWidth);

    Log.d("dinazaur coloane", "nr este: " + columns);
    Log.d("dinazaur randuri", "nr este: " + rows);


    if (columns == 4) {
        // Get 4 column widget remote view and return
        return new RemoteViews(context.getPackageName(),
                R.layout.simple_widget);
    } else {
        // Get appropriate remote view.
        return new RemoteViews(context.getPackageName(),
                R.layout.simple_widget2);
    }
}

/**
 * Returns number of cells needed for given size of the widget.
 *
 * @param size Widget size in dp.
 * @return Size in number of cells.
 */
private static int getCellsForSize(int size) {
    return (int) (Math.ceil(size + 30d) / 70d);
}

我不明白为什么在更改小部件布局大小后(变大 -> 更改为 layout_simple1,然后更小更改为 layout_simple back ),onUpdate() 处理 layout_simple 的 onClick PendingIntent 方法不再工作(不是甚至调用),代码是:

  @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int count = appWidgetIds.length;

    for (int i = 0; i < count; i++) {
        int widgetId = appWidgetIds[i];
        String number = String.format("%03d", (new Random().nextInt(900) + 100));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.simple_widget);
        remoteViews.setTextViewText(R.id.textView, number);

        Intent intent = new Intent(context, SimpleWidgetProvider.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);


    }
}

我的小部件 xml 是:

 <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/simple_widget"
    android:minHeight="60dp"
    android:minWidth="120dp"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="1800000"
    android:widgetCategory="home_screen|keyguard">

</appwidget-provider>

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    您需要在调用 updateAppWidget 之前重新分配 OnClickPendingIntent。注意描述public void partialUpdateAppWidget(int appWidgetId, RemoteViews views)“注意因为这些更新没有被缓存,所以他们修改的任何没有被restoreInstanceState恢复的状态都不会持续存在使用 AppWidgetService 中的缓存版本恢复小部件的情况。”

    例如如果您通过 RemoteView 更改小部件上的某些内容(不是全部),调用 updateAppWidget 并旋转屏幕,则 Widget 将仅恢复在 updateAppWidget 调用之前添加到小部件的数据。所有其他数据都将丢失。

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多