android widget listview 可点击

标签 android listview android-listview widget

我有一个带有 ListView 的小部件。我正在尝试将事件绑定(bind)到 ListView 中的列表项。我已经看过几个代码示例,但没有一个有效!

//小部件提供者

public class Widget extends AppWidgetProvider
{   

    public static String YOUR_AWESOME_ACTION = "com.my.assignmentmanager.YourAwesomeAction";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        for (int i = 0; i < appWidgetIds.length; i++)
        {
            Intent svcIntent = new Intent(context, WidgetService.class);

            svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

            widget.setRemoteAdapter(appWidgetIds[i], R.id.widgetListView, svcIntent);




            Intent toastIntent = new Intent(context, Widget.class);
            // Set the action for the intent.
            // When the user touches a particular view, it will have the effect of
            // broadcasting TOAST_ACTION.
            toastIntent.setAction(Widget.YOUR_AWESOME_ACTION);;
            toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);


            svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

            PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);              

            widget.setPendingIntentTemplate(R.id.widgetListView, toastPendingIntent);



            appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
        }

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

接收方法!应该可以抛出日志

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("debug", "onReceive");
        super.onReceive(context, intent);

        Log.d("debug", "onReceive");

    }       

}

小部件布局 xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/widgetListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="wqeqweqwew"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:visibility="gone" />

</FrameLayout>

Widget工厂的getViewAt方法

          @Override
public RemoteViews getViewAt(int rowNumber)
{

    RemoteViews remoteView = new RemoteViews(mContext.getPackageName(), R.layout.widget_listitem);
    remoteView.setTextViewText(R.id.widgetAssignmentTitle, mWidgetItems.get(0).assignmentTitle);
    remoteView.setTextViewText(R.id.widgetDaysLeft, mWidgetItems.get(0).assignmentTitle);

    Log.d("debug", "wwwwwwwwwwwwwwwwww");
    Bundle extras = new Bundle();
    extras.putInt("KEY", rowNumber);
    Intent fillIntent = new Intent();
    fillIntent.putExtra("Water", rowNumber);

    remoteView.setOnClickFillInIntent(R.layout.widget_listitem, fillIntent);

    return (remoteView);
}

我不知道,但在 Android 上,只需单击一下即可绑定(bind)真是令人头疼!我可以绑定(bind)数据,但不能绑定(bind)点击事件。

最佳答案

尝试改变这个:

remoteView.setOnClickFillInIntent(R.layout.widget_listitem, fillIntent);

类似于:

remoteView.setOnClickFillInIntent(R.id.listitemview, fillIntent);

关于android widget listview 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576873/

相关文章:

Android ListView : Doesn't highlight on touch, 但在我使用轨迹球时有效

Android:ListView 在其他元素之上

android - 我的 ListView 有两种类型的 View 。如何管理?

android - onPause 而不是 onSaveInstanceState

android - 为什么 Fragments 需要交易?

Android 通知内容文本不会显示在 OS 4.4.4 Kitkat 上,但会显示在 OS 5.0 Lollipop 上

android - 如何在android中的ListFragment中设置所选项目的颜色?

java - 自定义 ListView 未填充

android - 在 ListView 中更改 TextView 的文本时出现问题

java - Android Studio - 在按钮点击 WEAR 时播放声音