android获取gridview中点击项目的位置 [英] android get position of clicked item in gridview

查看:54
本文介绍了android获取gridview中点击项目的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们所知,使用 android 网格视图,我们可以执行以下操作并在单击项目时收到通知:

as we know using android grid view, we can do the following and get notified when item is clicked:

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Toast.makeText(PopularCapitActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});

我们也知道,如果网格中的单元格包含一个可点击的项目,比如一个按钮,上面的不会被触发.

we also know that, if the cell in the grid contains a clickable item, say a button, the above won't get fired.

所以目前我有一个网格视图,每个单元格都有自己的按钮,所以现在当用户点击按钮时,它将根据按钮所在的单元格有自己的动作,我的问题是,我如何访问按钮处理程序中的单元格位置?

so currently i have a grid view, each cell has its own button, so now when user clicks on the button, it will have its own action based on the cell the button resides in, my question is, how can i access the cell position in the button handler?

谢谢

推荐答案

假设您正在为 GridVIew 使用自定义适配器,在 getView 方法中,您可以简单地向包含传递到 getView 的位置的 Button 对象添加一个标记:

Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:

button.setTag(new Integer(position));

然后,在 onClickListener 方法中,使用传入的视图(按钮),您可以执行以下操作:

Then, in the onClickListener method, with the view that is passed in (the button) you can do:

Integer position = (Integer)view.getTag();

然后从那里处理位置值.

And then handle the position value from there.

看来最好的做法是:

button.setTag(Integer.valueOf(position));

而不是使用 Integer 构造函数.

rather than using the Integer constructor.

这篇关于android获取gridview中点击项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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