我已尽我最大的努力在包括 SO 在内的所有地方寻找解决方案,但似乎我无处可去,我决定问这个问题,看看我是否能找到能帮助我解决这个问题的人。好的,这就是我想要做的:
我有一个 4 列和 4 行的 GridView。GridView 使用 imageAdapter 填充图像。当连续选择一个图像时,该位置的图像会更改为另一个图像,当再次选择(取消选择)时,它会变回前一个图像。那也行。我现在要做的是,当一个位置被选中时,我想禁用所有其他位置,直到再次单击它来取消选择所选位置。本质上,我想确保不能同时选择两个位置。我已经尽力了,但现在这似乎有点不可能。谁能告诉我我做错了什么。当用户选择一个位置时,其他人不会连续被禁用。以下是我的代码: 任何帮助将不胜感激。感谢您的预期帮助。
私人 OnItemClickListener onGridItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//if (v == null) return; //Just added 03/04/2013
int rowOfSelectedItem = getRowInGrid(position);
Log.i(TAG, "Selected View is : "+v.getId()+" (Id arg = "+id+")- Position is "+ position+" Row is = "+rowOfSelectedItem);
switch(rowOfSelectedItem){
case 1:
//iterate through first row items and check if any of the items was selected before
for(int x=2; x < 4; x++){
//
Object tag = ((ImageView)gridView.getChildAt(x)).getTag();
Log.i(TAG, "This is : "+ x + "TAG is " + tag);
if(tag ==null || !Boolean.valueOf(tag.toString())){
//disable clicking so the user cannot select another image on same row
((ImageView)gridView.getChildAt(x)).setClickable(false);
((ImageView)gridView.getChildAt(x)).setEnabled(false);
Log.i(TAG, "This is : "+ x + tag);
}
}
break;
case 2:
for(int x=4; x < 8; x++){
//
Object tag = ((ImageView)gridView.getChildAt(x)).getTag();
if(tag ==null || !Boolean.valueOf(tag.toString())){
//disable clicking so the user cannot select another image on same row
((ImageView)gridView.getChildAt(x)).setClickable(false);
((ImageView)gridView.getChildAt(x)).setEnabled(false);
}
}
break;
case 3:
for(int x=8; x < 12; x++){
//
Object tag = ((ImageView)gridView.getChildAt(x)).getTag();
if(tag ==null || !Boolean.valueOf(tag.toString())){
//disable clicking so the user cannot select another image on same row
((ImageView)gridView.getChildAt(x)).setClickable(false);
((ImageView)gridView.getChildAt(x)).setEnabled(false);
}
}
break;
case 4:
for(int x=12; x < 16; x++){
//
Object tag = ((ImageView)gridView.getChildAt(x)).getTag();
if(tag ==null || !Boolean.valueOf(tag.toString())){
//disable clicking so the user cannot select another image on same row
((ImageView)gridView.getChildAt(x)).setClickable(false);
((ImageView)gridView.getChildAt(x)).setEnabled(false);
}
}
break;
}
私有 int getRowInGrid(int 位置){
if(position <= 3){
return 1;
}
else if(position <=7){
return 2;
}
else if(position <= 11){
return 3;
}
else if(position <= 15){
return 4;
}
else{ return -1;}
}