RemoteViewFactory onDataSetChanged()只调用一次notifyAppWidgetViewDataChanged()

问题描述:

我正在构建一个窗口小部件,用于为每个配方加载配料列表。我的目标是能够拥有多个窗口小部件实例,并独立加载/更新其成分列表(ListView)。我为用户设置了配置活动来选择配方。配置并填充remoteViews,使我的配料表中RemoteAdapter后,该方法将总是打电话:RemoteViewFactory onDataSetChanged()只调用一次notifyAppWidgetViewDataChanged()

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是

D/WidgetRemoteViewService: onGetViewFactory() call received with mRecipeId **1** 
D/WidgetRemoteViewsFactory: WidgetRemoteViewsFactory() constructed with recipeId: **1** 
D/WidgetRemoteViewsFactory: onDataSetChanged() called 

但你可以从上面的日志中看到,没有经过1(不像4这是预期的行为)

UI明智的,任何subsequentl发生y创建的小部件的listview实际上具有配方4(不是1)的成分,即使配置活动明显通过id为1 ... 2 ... 6 ..等等。

奇怪的是:?!后,才从主屏幕上删除我的所有部件,然后添加一个小部件,例如配方ID 1,那么它的onDataSetChanged()将被调用,并再次,任何后续部件的将不会被调用。

查看屏幕截图 Nutella Pie Recipe的ID是4,而Brownies Recipe的ID是1.首先添加Nutella Pie小部件,正确加载Nutella Pie的原料。布朗尼被添加第二位,正如你所见,成分从第一个结转到结束,而它们是不正确的。

一直想弄清楚过去的2天,在此先感谢。测试仿真器和真实设备> API 21,结果相同。任何帮助或暗示赞赏。

enter image description here

找到答案在这里通过@Joe onGetViewFactory only called once for multiple widgets

事实证明,这是一些关于如何RemoteViewService意图处理。我正在创建RemoteViewService的意图,就像我99。999%通常做任何活动,例如:

// Create intent to start the service and to act as the remote listView adapter 
Intent remoteViewIntent = new Intent(context, WidgetRemoteViewService.class); 
remoteViewIntent.putExtra(EXTRA_RECIPE_ID, recipeId); 
views.setRemoteAdapter(R.id.widget_recipe_ingredients_list, remoteViewIntent); 

解决方案:与其

remoteViewIntent.putExtra(EXTRA_RECIPE_ID, recipeId); 

将其更改为:

remoteViewIntent.setData(Uri.fromParts("content", String.valueOf(recipeId), null)); 

,并在RemoteViewsService的onGetViewFactory(),得到意向数据如下:

long mRecipeId = Long.valueOf(intent.getData().getSchemeSpecificPart());