我无法点击我的Widget。以下代码部分工作。我的小部件显示带有项目的ListView。当单击未显示项目的窗口小部件时,意图工作,活动开始。但是当点击ListView的项目时没有任何反应。
以下是代码: WidgetProvicer:
.Renviron
widget_layout.xml:
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
for (final int iD : appWidgetIds) {
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout
.widget_layout);
final Intent intent = new Intent(context, TickWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, iD);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.WidgetItem, intent);
remoteViews.setEmptyView(R.id.WidgetItem, R.id.empty_view);
final Intent activityIntent = new Intent(context, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);
appWidgetManager.updateAppWidget(iD, remoteViews);
appWidgetManager.notifyAppWidgetViewDataChanged(iD, R.id.WidgetItem);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
ListViewItems没有按钮或任何东西。
感谢您的帮助!
答案 0 :(得分:3)
最后它正在运作:
Profider onUpdate中的:
final Intent activityIntent = new Intent(context, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.WidgetItem, pendingIntent);
并在ViewsFactory中获取getViewAt:
Bundle infos = new Bundle();
infos.putInt(TickWidgetProvider.WIDGET_LISTID, this.listId);
final Intent activityIntent = new Intent();
activityIntent.putExtras(infos);
remoteView.setOnClickFillInIntent(R.id.widgetListItemLayout, activityIntent);
不知道我在哪里找到它,但是这个例子帮助我实现了它: https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget