小部件:区分具有相同 id 的视图的点击
我有一个简单的主屏幕小部件,它显示项目列表。由于我想支持较旧的设备,因此我使用了一系列“模拟”ListView 的 TextView 对象,而不是 ListView。
它工作得很好,但我想为这些项目分配一个点击侦听器。在侦听器中,我想区分它们并根据项目的内容采取行动。
如何做到这一点?由于 setOnClickPendingIntent 需要视图的 ID,因此我无法为各个项目分配单独的意图 - 它们都共享相同的 ID,因此处理程序不会知道哪个项目被点击。我只能从 XML 布局实例化 RemoteView,因此我无法添加具有不同 ID 的项目(我需要创建大量布局文件,仅布局 ID 不同)。
由于 Honeycomb 有 setOnClickFillInIntent
方法以可接受的方式处理我的问题,但如果我希望我的小部件在 Gingerbread 上工作,我无法使用它。
I've got a simple homescreen widget, which displays a list of items. Since I want to support older devices, instead of ListView I'm using a series of TextView objects that "emulate" ListView.
It works really well, but I'd like to assign a click listener for these items. In the listener I'd like to distinguish between them and take action depengind on item's content.
How to do this? Since setOnClickPendingIntent
takes view's ID, I can't assign separate intents for individual items - they all share the same ID, so the handler won't know which item was tapped. I can instantiate RemoteViews only from XML layout, so I can't add items with different ID's (I'd need to create a lot of layout files, differing only by layout ID).
Since Honeycomb there is setOnClickFillInIntent
method that deals with my problem in an acceptable way, but I can't use it if I want my widget to work on Gingerbread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的小部件必须具有唯一的 ID。
当然可以。每个应用程序小部件尺寸都有一个布局文件(因此,如果您只支持一种尺寸,则可能只有一个布局文件周期)。在该布局文件中,您为小部件指定唯一的 ID。
Your widgets have to have unique IDs.
Sure you can. You have one layout file per app widget size (possibly, therefore, only one layout file period if you are only supporting one size). In that layout file, you give your widgets unique IDs.
你也许可以!我遇到了同样的问题,但后来我意识到我一次生成一个行(布局中的各个行,来自同一模板的每一行)。每行都是它自己的 RemoteView,因此在 for 循环中,我将意图设置在其自身上。例如,如果我将新的innerRemoteViews添加到outerRemoteViews,我会调用:
这样,Intent就会添加到innerRemoteViews,其中id仍然特定于该行。
You might be able to! I had the same issue, but then I realized that I was generating the rows (individual rows within a layout, each row from the same template) one at a time. Each row was its own RemoteViews, so within that for loop, I set the intent on itself. For example, if I'm adding new innerRemoteViews to outerRemoteViews, I'd call:
This way, the Intent gets added to the innerRemoteViews, where the id is still specific to that row.