在GridView中单击项目的Andr​​oid开位置 [英] android get position of clicked item in gridview

查看:144
本文介绍了在GridView中单击项目的Andr​​oid开位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我们知道采用了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.

编辑: 看来最好的做法是这样:

It appears the best practice would be to do:

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

而不是使用整数构造。

rather than using the Integer constructor.

这篇关于在GridView中单击项目的Andr​​oid开位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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