如何将onClick侦听器附加到应用程序小部件上的listview项 [英] how attach a onClick Listener to an listview item on an app widget
本文介绍了如何将onClick侦听器附加到应用程序小部件上的listview项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢在列表视图的每个项目中添加一个onClick侦听器,但是我没有尝试过.
I like to add a onClick Listener to every item of my listview, but nothing what I have tried work.
这是我的RemoteViewsFactory:
Here is my RemoteViewsFactory:
public class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
[...]
public RemoteViews getViewAt(int position) {
final RemoteViews remoteView = new RemoteViews(
context.getPackageName(), R.layout.widget_item);
[...]
final Intent activityIntent = new Intent(context, ListActivity.class);
activityIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
activityIntent.putExtra(AppWidgetManager.ACTION_APPWIDGET_PICK, position);
PendingIntent pI = PendingIntent.getActivity(
context, appWidgetId, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteView.setOnClickPendingIntent(R.id.item_frame, pI);
return remoteView;
}
}
list-widget.xml
list-widget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<include layout="@layout/picture_frame" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/picframe"
android:layout_margin="5dp"
android:gravity="center"
android:loopViews="true" />
</RelativeLayout>
list-item.xml
list-item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_centerVertical="true" >
<TextView
android:id="@+id/date"
style="@style/WidgetItemText" />
<TextView
android:id="@+id/name"
style="@style/WidgetItemText" />
</LinearLayout>
如果我单击一个项目,则什么也没有发生.
If I click an item, nothing is happen.
如果我将意图直接悬挂在提供程序中并挂在列表视图上,则意图可以正常工作,但随后我将无法滚动并且不对单个项做出响应.
The intent works properly, if I hang it in the provider directly to the listview, but then I can no longer scroll and not respond to individual items.
推荐答案
查看全文