如何在单击按钮刷新时刷新窗口小部件列表视图?

时间:2016-09-08 10:35:15

标签: android listview android-widget

我在其上创建Widget ListView。显示Web ServiceListView的结果是可以的。我创建了一个类WidgetService,其中RemoteViewsService延伸到RemoteViewsFactoryListViewProvider我调用Web Service WidgetService。我添加了一个刷新按钮,单击它时会再次调用WidgetProvider来创建列表视图。

以下是我的public class WidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; for (int i = 0; i < N; ++i) { RemoteViews remoteViews = updateWidgetListView(context, appWidgetIds[i]); appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget); appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); } super.onUpdate(context, appWidgetManager, appWidgetIds); } private RemoteViews updateWidgetListView(Context context, int appWidgetId) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); //ListView Intent svcIntent = new Intent(context, WidgetService.class); svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent); remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view); //REFRESH PendingIntent updatePendingIntent = PendingIntent.getService(context, 0, svcIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.ivRefreshWidget, updatePendingIntent); return remoteViews; }

中的代码
PendingIntent

正如您在刷新部分中看到的那样,我尝试使用WidgetService来调用 int color = R.color.colorPrimaryDark; Drawable background = view.getBackground(); if (background instanceof ColorDrawable) color = ((ColorDrawable) background).getColor() ,但在测试时,它不会刷新列表。 感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

arrayList = new ArrayList<AllPostsProvider>();
arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());

并点击再次刷新按钮

 arrayList.clear();
 arrayList = new ArrayList<AllPostsProvider>();
 arrayList.add(all_contents);
AllPostReCyclerViewAdapter = new AllPostReCyclerViewAdapter(arrayList,getApplicationContext());

答案 1 :(得分:0)

在我的onUpdate我设置了一个要在onReceive中调用的动作,并将appWidgetIds用于onReceive

Intent refreshIntent = new Intent(context, WidgetProvider.class);
        refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

然后在onReceive我呼叫onUpdate

if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (appWidgetIds != null && appWidgetIds.length > 0) {
                Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
                this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
            }
        }
    }