在RemoteViews
for anAppWidget
中AdapterViewFlipper
,当用户单击AppWidget
. 根据官方文档,这应该通过在RemoteViews
. 然后AppWdiget
应该使用 partialUpdateAppWidget 更新,它甚至具有showNext()
文档中的示例功能。我已经尝试过实现这一点,但我很可能会犯一些我似乎无法弄清楚的愚蠢错误。
public class WidgetProvider extends AppWidgetProvider {
public static final String NEXT = "next";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent nextIntent = new Intent(context, WidgetProvider.class);
nextIntent.setAction(NEXT);
views.setOnClickPendingIntent(R.id.next_button,
PendingIntent.getBroadcast(context, appWidgetId, nextIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
views.setRemoteAdapter(R.id.view_flipper, new Intent(context,
WidgetRemoteViewService.class));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(NEXT)) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
remoteViews.showNext(R.id.view_flipper);
AppWidgetManager.getInstance(context).partiallyUpdateAppWidget(
AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, WidgetProvider.class)),
remoteViews);
}
super.onReceive(context, intent);
}
}
当用partiallyUpdateAppWidget
下updateAppWidget
一个按钮替换时,直到方向改变或手机进入睡眠状态,这会强制重绘最后存储的RemoteViews
. 这让我相信我犯的错误与部分更新有关,但我无法弄清楚我做错了什么。