我正在使用GridView开发一个项目,如果用户点击这两个具有相同图像的项目,我需要隐藏项目,然后两个项目都应该隐藏。
第一次加载项目时,所有项目都会有一个问号图像。
当用户点击项目时,背景图片会显示,如果他点击了另一个具有相同图片的项目,那么如果没有这两个项目应该隐藏,它会再次显示问号图像。
这怎么可能?
这是我的MainActivity.java
public class MainActivity extends Activity {
Context ctx;
int imagesArray[];
GridViewContent adapter;
List<Integer> pictures;
boolean flage=false;
int img1=-1,img2=-1;
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
shuffleArray();
adapter= new GridViewContent(this);
final GridView grid = (GridView) findViewById(R.id.gv_memory);
grid.setAdapter(new GridViewContent(this));
}
private void shuffleArray() {
// TODO Auto-generated method stub
pictures= new ArrayList<Integer>();
for (int index = 0; index < OriginalArray.length; index++)
{
pictures.add(OriginalArray[index]);
}
Collections.shuffle(pictures);
}
public class GridViewContent extends BaseAdapter {
private Context context;
//abstract change();
public int pictureArray[]={
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
};
public GridViewContent(Context c){
context=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureArray.length);
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureArray[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
convertView=LayoutInflater.from(context).inflate(R.layout.main, null);
final ImageView myimage=new ImageView(context);
myimage.setImageResource(pictureArray[position]);
//myimage.setImageResource(pictures.get(pictureArray[position]));
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
myimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// int post1,post2;
myimage.setImageResource(pictures.get(position));
if(flage==false)
{
img1=pictures.get(position);
flage=true;
}else if(flage==true){
//post2=position;
img2=pictures.get(position);
checkResult();
//notifyDataSetChanged();
flage=false;
}
//else if(f)
}
});
return myimage;
}
}
public void checkResult() {
if(img1==img2)
{
//pictures.remove(post2);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Congratualatin !!!!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "Sorry!!!!", Toast.LENGTH_LONG).show();
final GridView grid = (GridView) findViewById(R.id.gv_memory);
grid.setAdapter(new GridViewContent(this));
}
}
}
答案 0 :(得分:1)
您可以尝试记住单击的GridView
项的索引。发生第二次单击时,请检查记住的索引。如果填充(意味着用户已经单击任意图像一次),从适配器中提取两个drawable并检查它们是否相等。如果是,请从适配器中删除它们并进行更新。不要忘记重置记住的索引。
在代码中,这看起来像这样:
int firstClickIndex = -1;
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
if (-1 == firstClickIndex) {
firstClickIndex = position;
} else {
final int fstDrawable = adapter.get(firstClickIndex);
final int sndDrawable = adapter.get(position);
if (fstDrawable == sndDrawable) {
// Remove stuff, then reset firstClickIndex.
} else {
// Reset firstClickIndex.
}
}
}
});