【发布时间】:2018-03-06 03:35:45
【问题描述】:
我正在构建一个小部件来加载每个食谱的成分列表。我的目标是能够拥有多个小部件实例并独立加载/更新它们的成分列表(ListView)。我已经为用户设置了一个配置活动来选择配方。在配置和填充 remoteViews 并为我的成分列表制作 RemoteAdapter 之后,该方法将始终调用:
appWidgetManager.updateAppWidget(mAppWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.widget_list_view_layout);
updateAppWidget 很好地更新了非集合视图,但是,我对我的集合视图(成分列表)的 notifyAppWidgetViewDataChanged() 感到不满。
我第一次将小部件添加到屏幕(recipeId 4)时,它会正确加载并调用正确的服务和视图工厂来填充远程 listView。但如果我添加第二个,会发生以下情况(或缺少):
D/WidgetUtils: createAppWidgetResult() with appWidgetId: 78, recipeId: 4
D/WidgetUtils: RecipeId of the ingredients passed: 4
D/WidgetRemoteViewService: onGetViewFactory() call received with mRecipeId 4
D/WidgetRemoteViewsFactory: WidgetRemoteViewsFactory() constructed with recipeId: 4
D/WidgetRemoteViewsFactory: onDataSetChanged() called
D/WidgetUtils: createAppWidgetResult() with appWidgetId: 79, recipeId: 1
D/WidgetUtils: RecipeId of the ingredients passed: 1
D/WidgetUtils: createAppWidgetResult() with appWidgetId: 80, recipeId: 2
D/WidgetUtils: RecipeId of the ingredients passed: 2
日志结束
如果配方 ID 为 1
,我期待以下内容D/WidgetRemoteViewService: onGetViewFactory() call received with mRecipeId **1**
D/WidgetRemoteViewsFactory: WidgetRemoteViewsFactory() constructed with recipeId: **1**
D/WidgetRemoteViewsFactory: onDataSetChanged() called
但正如您从上面的日志中看到的那样,在 1 之后什么也没有发生(与 4 不同,这是预期的行为)
UI 方面,任何随后创建的小部件的列表视图实际上都具有配方 4(不是 1)的成分,即使配置活动清楚地将 id 传递为 1...2...6..etc。
奇怪的是:只有在我从主屏幕上删除所有小部件,然后添加一个小部件,例如配方 id 1 之后,它的 onDataSetChanged() 才会被调用?!,然后再一次,任何后续的小部件都不会被调用。
查看屏幕截图 Nutella Pie Recipe 的 id 为 4,Brownies 食谱的 id 为 1。 Nutella Pie 小部件首先添加,Nutella Pie 的成分已正确加载。布朗尼是第二个添加的,正如您所看到的,成分是从第一个中继承的,它们是不正确的。
在模拟器和真实设备上测试 > API 21,结果相同。
【问题讨论】:
标签: android listview widget android-widget