【问题标题】:Android Appwidget: addOnClickPendingIntent for RemoteViews in a ViewFlipperAndroid Appwidget:在 ViewFlipper 中为 RemoteViews 添加OnClickPendingIntent
【发布时间】:2011-12-27 11:27:34
【问题描述】:

我正在尝试将多个视图添加到 ViewFlipper,它位于 AppWidget(主屏幕小部件)中。 这是我的代码:

for (Item item : list) {
    RemoteViews rv = new RemoteViews(getPackageName(),
            R.layout.widget_item);

    rv.setTextViewText(R.id.txtTitle, item.getTitle());

    Intent launchIntent = new Intent(this, ActItemViewer.class);
    launchIntent.putExtra("id", item.getId());

    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, id, launchIntent, 0);
    rv.setOnClickPendingIntent(R.id.txtTitle, pendingIntent);

    views.addView(R.id.viewFlipper, rv);
}

此代码运行良好,所有项目都显示在我的小部件中。 但是当我单击 ViewFlipper 中的一个项目时,ActItemViewer 总是以相同的“id”打开。所以我认为只设置了第一个 PendingIntent。

有什么方法可以为 ViewFlipper 中的每个子级设置不同的 onClick 事件? 我见过一些具有此功能的 Widget,例如“Spiegel Online”-Widget(德语)或 Market-Widget。

【问题讨论】:

    标签: android onclick viewflipper android-appwidget android-pendingintent


    【解决方案1】:

    一个不需要随机性的更简单的解决方案是,您可以像这样将所有额外内容嵌入到数据字符串中,以使意图独一无二:

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

    【讨论】:

      【解决方案2】:

      我找到了解决方案here。这非常有效。

      我只需要使用launchIntent.setAction("" + Math.random()) 来使每个 Intent 对系统都是唯一的。 (额外内容不会使 Intent 独一无二,因为 Android 不会检查它们是否不同)。

      【讨论】:

      • 这既不能保证是唯一的,也不实用。应该遵循 arne.jans 的回答
      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多