【发布时间】:2014-03-13 09:13:12
【问题描述】:
我有这个homescreen widget:
很容易更新例如两个buttons 旁边来自activity 的标志,因为我有他们的appWidgetIds 来自AppWidgetProvider 的onUpdate 方法,但列表中的imagebuttons 是在RemoteViewsFactory 类中创建的getViewAt() 方法。
@Override
public RemoteViews getViewAt(int position) {
RemoteViews row = new RemoteViews(ctxt.getPackageName(), R.layout.list_row_widget);
row.setTextViewText(R.id.tvDescription, items.get(position).name);
Intent i = new Intent();
Bundle extras = new Bundle();
extras.putBoolean(TimeWidgetProvider.ACTION_WORK_START, true);
extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
i.putExtras(extras);
row.setOnClickFillInIntent(R.id.btnStartWork, i);
return (row);
}
所以它们每个都有相同的 id (R.id.btnStartWork),以及相同的 appWidgetId,它不是行 id,而是整个小部件 id。
我需要更改我单击的行中的 imagebutton,但是因为我在 Activity 中只有一个 appwidget id,并且它用于整个小部件,所以每次我使用时:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_layout);
views.setImageViewResource(R.id.btnStartWork, R.drawable.button_active);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
它会更改第一个图像按钮而不是选定的按钮。
我试图从工厂传递 remoteviews 行,但它不起作用,因为当我调用时
appWidgetManager.updateAppWidget(mAppWidgetId, row);
使用整个小部件的 id,小部件消失了,只有选定的行可见。我可以获取列表中项目的位置,但这没有帮助,因为它不是 viewId。 我已经阅读了很多教程,包括:http://developer.android.com/guide/topics/appwidgets/index.html
但仍然没有找到答案,这是我的第一个小部件。请帮忙。
谢谢 ;)
【问题讨论】:
标签: android android-appwidget remoteview