更新具有多个布局的小部件

时间:2015-09-01 09:08:38

标签: android android-widget

我有一个应用程序,需要根据大小更改窗口小部件的不同布局,例如,如果窗口小部件是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),处理onClick PendingIntent for layout_simple的onUpdate()方法无法正常工作任何更多(甚至没有被调用),代码是:

  @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>

1 个答案:

答案 0 :(得分:0)

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

E.g。如果您通过RemoteView更改了窗口小部件上的某些内容(并非全部),请调用updateAppWidget并旋转屏幕,然后Widget将仅恢复在updateAppWidget调用之前添加到窗口小部件的数据。所有其他数据都将丢失。