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

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

问题描述

我在本教程。我希望能够点击一个单一的形象,做其他活动,我需要知道什么样的形象被点击。

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.

我一定要添加imageView.onKeyDown(键code,事件)在ImageAdapter类?这里是code,因为它目前存在的:

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?

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

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