简体   繁体   English

在Android中的GridView中创建可单击的图像

[英]Create a clickable image in a GridView in Android

I have images displayed in a GridView as in this tutorial . 我在本教程中将图像显示在GridView中。 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? 我是否必须在ImageAdapter类中添加imageView.onKeyDown(keyCode,event)? 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? 如何创建正确的处理程序?

For a GridView you can use the setOnItemClickListener method to have an OnItemClickListener listener. 对于GridView,您可以使用setOnItemClickListener方法来拥有OnItemClickListener侦听器。 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? 这就是你需要的吗?

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. 我尝试了上面提到的方法getView(final int position ...)意识到位置在28个项目之后得到“重置”,并且在gridview中第28个项目之后回到0的位置。

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. 下面是一个示例代码,在展示gridview的活动中调用click事件。

public class MainActivity extends Activity {

ArrayList<Integer> item_ids = new ArrayList<Integer>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    item_ids = //get your item ids method

    GridView gridview = (GridView) findViewById(R.id.grid_featured);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(itemClickListener);

    footer = (Footer)findViewById(R.id.layoutFooter);
    footer.setActivity(this);
}

private OnItemClickListener itemClickListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Log.d(TAG,"Position Clicked ["+position+"] with item id ["+item_ids.get(position)+"]");
    }
};

} }

I was able to get the position of the clicked image by making the position final and adding an onClick listener to the imageView. 通过使位置最终并向imageView添加onClick侦听器,我能够获得所点击图像的位置。 This logs the position of the image that was clicked. 这会记录所单击图像的位置。

@Override
public View getView(final 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);


    imageView.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        Log.d("onClick","position ["+position+"]");
      }

    });

  } 
  else {
    imageView = (ImageView) convertView;
  }

  imageView.setImageResource(mThumbIds[position]);
  return imageView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM