在 Android 的 GridView 中创建可点击的图像 [英] Create a clickable image in a GridView in Android

查看:35
本文介绍了在 Android 的 GridView 中创建可点击的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GridView 中显示图像,如 本教程.我希望能够点击单个图像并执行其他事件,我需要知道点击了哪个图像.

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.

我必须在 ImageAdapter 类中添加 imageView.onKeyDown(keyCode, event) 吗?这是当前存在的代码:

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?

推荐答案

对于 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

onItemClick(AdapterView<?> parent, View v, int position, long id)

获取项目在被单击的网格中的位置以及网格单元格内的视图的位置.这是你需要的吗?

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?

这篇关于在 Android 的 GridView 中创建可点击的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆