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

发布于 2024-07-17 21:34:03 字数 920 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-07-24 21:34:03

对于 GridView,您可以使用 setOnItemClickListener 方法来拥有 OnItemClickListener 侦听器。 该侦听器将为您提供一个必须使用签名覆盖的方法,

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

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

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?

深白境迁sunset 2024-07-24 21:34:03

我能够通过将位置设置为最终位置并将 onClick 侦听器添加到 imageView 来获取单击图像的位置。 这会记录被单击的图像的位置。

@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;
}

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.

@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;
}
孤独陪着我 2024-07-24 21:34:03

我尝试了上面提到的方法 getView(final int position . . .)
意识到在 28 个项目之后位置被“重置”,并且在 gridview 中的第 28 个项目之后回到 0 时位置。

我怀疑最终关键字造成了问题,删除它后我能够按预期获得位置。

下面是一个示例代码,其中在展示 gridview 的活动中调用了单击事件。

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 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.

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)+"]");
    }
};

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文