android - 如何通知Android Widget重绘

标签 android android-widget android-appwidget remoteview

我有一个使用 RemoteViewsFactory 的 Android Widget 来填充 ListView。

我的数据源是一个异步 JSON 调用,可以随时完成(它不是推送通知)。现在代码位于 onDataChanged 中,看起来像这样:

@Override
public void onDataSetChanged() {

    // Subsequent calls to get the data.
    // Called when the widget onReceive ACTION_APPWIDGET_UPDATE procs.
    newsGetter.updateListFeed(null, new NewsGetter.OnUpdateListFeedFinishedListener() {
        @Override
        public void onUpdateListFeedFinished(VolleyError error) {
            //called when async call has finished. not sure what to call here?
        }
    });

    Log.e(TAG, "******************************** onDataSetChanged PROVIDER");
}

每小时(我已将 updatePeriodMillis 设置为每小时更新一次)我的 AppWidgetProvider 收到一个请求更新的 onReceive 调用(ACTION_APPWIDGET_UPDATE) 这样我上面的代码就运行了。但是,在我的代码运行并更新 feedItems 之后,即使在滚动时,UI 也不会更新。

所以我的问题是:如何使 UI 无效并告诉它重绘所有 View ?

到目前为止我已经尝试过:

appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_list);

但这也会调用 onDataChanged,这会导致无限循环,所以我不能使用它。

最佳答案

此问题的解决方案是使用 LocalBroadcastManager 将消息从 RemoteViewsFactory 发送到 AppWidgetProvider。有关示例,请参见此处: Android onReceive not called

关于android - 如何通知Android Widget重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35262727/

相关文章:

java - 如何在 android 小部件中设置泰米尔语文本?

安卓数字输入加减号

android - Widget 中的 ListView + GridView

android - 嵌套 ScrollView 高度问题

android - 使用 SimpleDateFormat 格式化日期时出现 NullPointerException

android - 将自定义字体设置为对话框

android - 通知中的缩放、裁剪图标

android - 升级 Google Play Services 7.5.0 后 RatingBar 颜色显示为白色

android - 如何获取已安装的widget列表?

android - GridView 小部件中的重复项目