每个notifyAppWidgetViewDataChanged()仅调用一次RemoteViewFactory onDataSetChanged() [英] RemoteViewFactory onDataSetChanged() only called once per notifyAppWidgetViewDataChanged()

查看:525
本文介绍了每个notifyAppWidgetViewDataChanged()仅调用一次RemoteViewFactory onDataSetChanged()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个小部件,以加载每个配方的配料列表.我的目标是能够具有该小部件的多个实例,并使其成分列表(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角度来看,即使配置活动明确将id传递为1 ... 2 ... 6..etc,任何随后创建的小部件的listview实际上都具有配方4(而不是1)的成分.

足够奇怪:仅当我从主屏幕上删除所有小部件,然后添加一个小部件(例如配方ID 1)之后,将再次调用其onDataSetChanged()! ,则不会调用任何后续小部件.

查看屏幕快照:Nutella Pie食谱的ID为4,Brownies Recipe的ID为1.首先添加了Nutella Pie小部件,正确加载了Nutella Pie的配料.布朗尼蛋糕是第二个添加的,如您所见,配料是从第一个配料中结转的,它们是不正确的.

在仿真器和真实设备上进行了测试> API 21,结果相同.

解决方案

@Joe在这里找到了答案

,然后在RemoteViewsService的onGetViewFactory()中,从意图中获取数据,如下所示:

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

I'm building a widget to load a list of ingredients per recipe. My goal is to be able to have multiple instances of the widget and have their ingredient-list (ListView) loaded/updated independently. I've setup a configuration activity for user to select the recipe. After configuration and populating the remoteViews and making the RemoteAdapter for my list of ingredients, the method would always call:

appWidgetManager.updateAppWidget(mAppWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.widget_list_view_layout);

updateAppWidget updates the non-collection views nicely, however, I'm having a beef with notifyAppWidgetViewDataChanged() for my collection view, the list of ingredients.

The first time I add the widget to the screen (recipeId 4), it loads correctly and calls the correct service and view factory to populate the remote listView. But if I add the second one, here is what happens (or lack thereof):

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

End of log

I was expecting the following, if the recipe id is 1

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

But as you can see from the above Log, nothing happens after 1 (unlike with 4 which is the expected behavior)

UI wise, any subsequently created widget's listview actually has the ingredients for the recipe 4 (not 1), even when the configuration activity clearly passes the id as 1...2...6..etc.

Strangely Enough: Only After I remove all my widgets from the home screen, and then add ONE widget, for example recipe id 1, then its onDataSetChanged() would be called?!, and again, any subsequent widget's won't be called.

See the screenshot The Nutella Pie Recipe's id is 4, and the Brownies Recipe's id is 1. The Nutella Pie widget is added first, the ingredients for the Nutella Pie is correctly loaded. The Brownies, is added second, and as you can see, the ingredients are carried over from the first one, and they are incorrect.

Tested on emulator and real devices > API 21, same outcome.

解决方案

Found the answer here by @Joe onGetViewFactory only called once for multiple widgets

As it turns out, it is something about how RemoteViewService intent handling. I was creating the RemoteViewService intent like what I 99.999% normally do in any activity, ex:

// 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);

Solution: Instead of

 remoteViewIntent.putExtra(EXTRA_RECIPE_ID, recipeId);

Change it to:

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

and in the RemoteViewsService's onGetViewFactory(), get the data from the intent like so:

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

这篇关于每个notifyAppWidgetViewDataChanged()仅调用一次RemoteViewFactory onDataSetChanged()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆