小编Woo*_*iee的帖子

如何将 onClick 侦听器附加到应用小部件上的列表视图项

我喜欢向我的列表视图的每个项目添加一个 onClick 侦听器,但我尝试过的一切都不起作用。

这是我的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

列表小部件.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>
Run Code Online (Sandbox Code Playgroud)

列表项.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …
Run Code Online (Sandbox Code Playgroud)

android listview android-appwidget

4
推荐指数
1
解决办法
2791
查看次数

标签 统计

android ×1

android-appwidget ×1

listview ×1