gpt4 book ai didi

java - 如果使用 Android 的两个项目具有相同的图像,则隐藏 GridView 中的项目

转载 作者:行者123 更新时间:2023-12-01 13:17:11 25 4
gpt4 key购买 nike

我正在使用 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));
}

}
}

最佳答案

您可以尝试记住您单击的 GridView 项目的索引。当第二次单击发生时,检查记住的索引。如果它已填充(意味着用户已经单击了任 Intent 像一次),则从适配器中提取两个可绘制对象并检查它们是否相等。如果是,请将它们从适配器中删除并更新。不要忘记重置记住的索引。

在代码中,这可能看起来像这样:

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.
}

}
}
});

关于java - 如果使用 Android 的两个项目具有相同的图像,则隐藏 GridView 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com