Android 多列列表/网格与适配器和 OnItemClickListener?
我有一个简单的网格,其中包含项目行和每个项目上的选项列。当然,我可以通过特定的资源 ID 手动将 OnClick 操作链接到单元格项目:
views.setOnClickPendingIntent(R.id.row1column1, launchA);
views.setOnClickPendingIntent(R.id.row1column2, launchB);
// ...
views.setOnClickPendingIntent(R.id.row2column1, launchC);
// ...
我更喜欢使用 ListView 或 GridView 和适配器。有没有办法从其中之一获取手机 ID?我希望 ListView setOnItemClickListener() 会用行“位置”内视图的视图 id 来调用我:
onItemClick(AdapterView<?> parent, View view, int position, long id) { ... }
但没有。也许有不同的方法?
谢谢。
更新:
我从这个开始:
contactsView.setAdapter(new ContactAdapter(this.getApplicationContext(), R.layout.contact, contacts));
contactsLV.setOnItemClickListener(new OnItemClickListener() { ... });
我假设我无法进入我的适配器并设置子视图 OnClickListeners。错误的假设。当我回过头来尝试一下时:
Button x = (Button)newView.findViewById(R.id.contact_x);
Button y = (Button)newView.findViewById(R.id.contact_y);
x.setOnClickListener(new OnClickListener() { ... });
y.setOnClickListener(new OnClickListener() { ... });
...
瞧!
I have a simple grid with rows of items and columns of options on each item. Of course I can manually link OnClick actions to cell items through specific resource IDs:
views.setOnClickPendingIntent(R.id.row1column1, launchA);
views.setOnClickPendingIntent(R.id.row1column2, launchB);
// ...
views.setOnClickPendingIntent(R.id.row2column1, launchC);
// ...
What I'd prefer to to is use a ListView or a GridView and an Adapter. Is there a way to get cell id's from one of those? I was hoping that ListView setOnItemClickListener() would call me with the view id of a view within row 'position':
onItemClick(AdapterView<?> parent, View view, int position, long id) { ... }
but no go. Maybe a different approach?
Thanks.
Update:
I started with this:
contactsView.setAdapter(new ContactAdapter(this.getApplicationContext(), R.layout.contact, contacts));
contactsLV.setOnItemClickListener(new OnItemClickListener() { ... });
I'd assumed that I couldn't go into my Adapter and set child view OnClickListeners. Bad assumption. When I went back at this I gave it a try:
Button x = (Button)newView.findViewById(R.id.contact_x);
Button y = (Button)newView.findViewById(R.id.contact_y);
x.setOnClickListener(new OnClickListener() { ... });
y.setOnClickListener(new OnClickListener() { ... });
...
Voila!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论