在 Android 的 GridView 中创建可点击的图像
我在 GridView 中显示图像,如本教程中所示。 我希望能够单击单个图像并执行其他事件,并且我需要知道单击了哪个图像。
我是否必须在 ImageAdapter 类中添加 imageView.onKeyDown(keyCode, event) ? 这是当前存在的代码:
@Override
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.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
//does this need imageView.onKeyDown(keyCode, event)?
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
它将如何指示单击了哪个图像? 如何创建正确的处理程序?
I have images displayed in a GridView as in this tutorial. I want to be able to click on a single image and do other events and I need to know what image was clicked.
Do I have to add imageView.onKeyDown(keyCode, event) in the ImageAdapter class? Here is the code as it currently exists:
@Override
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.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
//does this need imageView.onKeyDown(keyCode, event)?
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
How will it indicate what image was clicked? How do I create the proper handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 GridView,您可以使用 setOnItemClickListener 方法来拥有 OnItemClickListener 侦听器。 该侦听器将为您提供一个必须使用签名覆盖的方法,
您可以在其中获取单击的网格中的项目的位置以及网格单元格内的视图。 这是你需要的吗?
For a GridView you can use the setOnItemClickListener method to have an OnItemClickListener listener. That listener will give you a method that you must override with the signature
where you get the position of the item in the grid that was clicked and the View that is inside the cell of the grid. Is that what you need?
我能够通过将位置设置为最终位置并将 onClick 侦听器添加到 imageView 来获取单击图像的位置。 这会记录被单击的图像的位置。
I was able to get the position of the clicked image by making the position final and adding an onClick listener to the imageView. This logs the position of the image that was clicked.
我尝试了上面提到的方法 getView(final int position . . .)
意识到在 28 个项目之后位置被“重置”,并且在 gridview 中的第 28 个项目之后回到 0 时位置。
我怀疑最终关键字造成了问题,删除它后我能够按预期获得位置。
下面是一个示例代码,其中在展示 gridview 的活动中调用了单击事件。
}
i tried the above mentioned method getView(final int position . . .)
realized that the position got "reset" after 28 items and the position when back to 0 after the 28th item in the gridview.
i suspected the final keyword is creating problem and after removing it i was able to get the positions as expected.
Below is a sample code with the click event being called in the activity that is showcasing the gridview.
}