Android:在 Gridview 中如何使图像可点击
我试图使图像可点击,以便当按下它们时会将用户发送到另一个页面或链接。
我怎样才能实现这个目标?目前,GridView
中的每一行都有 2 个按钮
。它如何知道 GridView 中的哪个项目被单击,以便它执行特定于被单击的项目的特定操作。
感谢您的帮助!
这是我的图像适配器类中的:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setAdjustViewBounds(true);
// imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(4, 8, 4, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.menu_about,R.drawable.menu_episodes
};
}
I am trying to make the images clickable so that when they are pressed it will send the user to another page or link.
How can I achive this? Currently each row in the GridView
has 2 Buttons
. How will it know which item in the GridView
is clicked so that it performs a certain action, specific to the item that was clicked.
Thanks for any help!
This is in my image adapter class:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setAdjustViewBounds(true);
// imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(4, 8, 4, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.menu_about,R.drawable.menu_episodes
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需向 GridView 添加一个侦听器
Just add a listener to the GridView
您可能需要考虑使用 ImageButton 类来为您执行此操作。它是一个按钮,但不是有无聊的灰色渐变,而是在其中放置图像!
Andriod ImageButton 文档
从那里您可以自由地使用点击侦听器,就像使用常规按钮一样。
You may want to consider using the ImageButton class to do this for you. Its a button but instead of having a boring gray gradient you place an image there instead!
Andriod ImageButton Docs
From there you are free to use a click listener just like you would with a regular button.
首先找到按钮的位置(此处图片),然后使用开关盒执行您所说的操作...
first of all find the position of the buttons(Here Image) and then use the switch case to perfrom what ever you say...