带有ListView的Android可点击小部件在ListItems上不可点击 [英] Android clickable Widget with ListView not clickable on ListItems
问题描述
我无法使我的窗口小部件可点击.以下代码部分起作用.我的窗口小部件显示带有项目的ListView.当单击不显示任何项目的窗口小部件时,该Intent起作用并且Activity开始.但是,当单击ListView的项目时,没有任何反应.
I am having trouble making my Widget clickable. The following code is working partially. My Widget shows a ListView with Items. When clicking into the Widget where no Item is displayed, the Intent works and the Activity starts. But when clicking on an Item of the ListView nothing happens.
以下是代码: WidgetProvicer:
Here is the Code: WidgetProvicer:
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);
}
widget_layout.xml:
widget_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/WidgetItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#D3D3D3"
android:dividerHeight="1dp"/>
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="@string/emptytext"
android:visibility="gone"/>
</LinearLayout>
ListViewItems没有按钮或其他任何东西.
The ListViewItems have no Buttons or anything.
感谢您的帮助!
推荐答案
终于可以了:
在Profider onUpdate中:
in the 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:
and in the 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
Do not know where I found it, but this example helped me making it work: https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget
这篇关于带有ListView的Android可点击小部件在ListItems上不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!