【问题标题】:android widget listview clickableandroid 小部件列表视图可点击
【发布时间】:2014-10-26 19:17:19
【问题描述】:

我有一个带有列表视图的小部件。我正在尝试将事件绑定到列表视图中的列表项。我已经通过了几个代码示例,它们都不起作用!

// 小部件提供者

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);
    }

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

    @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 上绑定一个点击是一件很头疼的事情!我可以绑定数据但不能绑定点击事件。

【问题讨论】:

  • 你能解决这个问题吗?我也遇到了同样的问题

标签: android listview android-listview widget


【解决方案1】:

尝试改变这个:

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

类似于:

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

【讨论】:

猜你喜欢
  • 2011-11-24
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 2011-04-22
  • 2012-10-28
  • 1970-01-01
相关资源
最近更新 更多