2015-09-01 67 views
0

我有一個應用程序需要更改不同的小部件佈局,取決於大小,例如,如果小部件是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回),onUpdate()方法處理的onClick的PendingIntent爲layout_simple不工作的話(甚至不叫),代碼:

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

回答

0

您需要在updateAppWidget調用之前重新分配您的OnClickPendingIntent。注意說明public void partiallyUpdateAppWidget(int appWidgetId,RemoteViews視圖)「請注意,因爲這些更新沒有被緩存,所以它們修改的任何狀態都不會被restoreInstanceState恢復,在這些小部件被恢復的情況下將不會持續使用AppWidgetService中的緩存版本「。

例如,如果您通過RemoteView更改widget上的某些內容(不是全部),調用updateAppWidget並旋轉屏幕,則Widget將只會恢復在updateAppWidget調用之前添加到該widget的數據。所有其他數據將丟失。