我在其上创建Widget
ListView
。显示Web Service
中ListView
的结果是可以的。我创建了一个类WidgetService
,其中RemoteViewsService
延伸到RemoteViewsFactory
,ListViewProvider
我调用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()
,但在测试时,它不会刷新列表。
感谢您的帮助。
答案 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);
}
}
}