如何grid-view
通过它的位置获取一个项目标签,并且必须从另一个grid-view
项目点击监听器函数中获取。
gridview2.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
int i=(Integer)gridview1.getChildAt(gridview1s_position).getTag();---> it is returning Null
}
});
MYgridview1.setOnitemClicklistener
和gridview2.setonitemclicklistener
在同一个文件中 ( MainActivity.java
)
但是gridview1
适配器是一个ImageAdapter.java
(这是用于设置gridview1中的图像)和gridview2
适配器是imageadapter2.java
(这是用于设置黑色图像,以便gridview2
看起来像一个带有行和列的框,否则它显示为粗线,因为我们没有'不要在gridview中放任何东西)
gridview1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
int tag=(Integer)gridview1.getChildAt(position).getTag();//this returning the tag
}
});
ImageAdapter1.java
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
if(arg1!=null)
{
imv=(ImageView)arg1;
}
else
{
imv=new ImageView(cont);
imv.setLayoutParams(new GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
imv.setPadding(0,0,0,0);
}
imv.setImageResource(imageid[arg0]);//imageid aray is having drawable images
imv.setTag(imageid[arg0]);
return imv;
}
网格视图2
gridview2.setOnItemClickListener(new OnItemClickListener()
{
ImageAdapter ia=new ImageAdapter(getApplicationContext());
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
ImageView imv;
if(v!=null)
{
imv=(ImageView)v;
}
else
{
imv=new ImageView(getApplicationContext());
imv.setLayoutParams(new GridView.LayoutParams(40,40));
imv.setScaleType(ScaleType.CENTER_CROP);
imv.setPadding(0,0,0,0);
imv.setBackgroundColor(5555);
}
imv.setImageResource(ia.imageid[tt]);
}
});