gpt4 book ai didi

java - 如何以特定的项目点击次数翻转 GridView 中的所有项目

转载 作者:行者123 更新时间:2023-12-01 14:01:45 24 4
gpt4 key购买 nike

我正在尝试做一种翻页卡应用程序,其中有显示相同图像的图像 GridView ,例如card_back。

单击每个图像时,它会翻转显示另一个图像,该图像对于 gridview 的每个项目都不同,例如card_front。

我想做的是,如果显示两张卡片正面,则单击下一个项目时,前两张卡片应翻转以显示其卡片背面图像。即,一次最多只能显示两张卡片他们的card_front图像。但我不知道该怎么做。

有人可以帮我吗。提前致谢。

这是在gridview中翻转图像的代码。

   public class FullscreenActivity extends Activity
implements OnItemClickListener, AnimationListener

{ ImageView imageView ;
int pos;
Animation animation1;
Animation animation2;
int[] clicked = {0,0,0,0,0,0,0,0,0,0,0,0};



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
animation1.setAnimationListener(this);

animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
animation2.setAnimationListener(this);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
pos=position;
imageView = ((ViewHolder) v.getTag()).img;

(imageView).clearAnimation();
(imageView).setAnimation(animation1);
(imageView).startAnimation(animation1);

}

});
}


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub

}


@Override
public void onAnimationEnd(Animation arg0) {

if (arg0==animation1) {

if(clicked[pos]==0)
{
imageView.setImageResource(ImageAdapter.mThumbSelected[pos]);

clicked[pos]=1;

} else if(clicked[pos]==1) {
clicked[pos]=0;

imageView.setImageResource(R.drawable.card_back);
}

imageView.clearAnimation();
imageView.setAnimation(animation2);
imageView.startAnimation(animation2);
}

}


@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}


@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}



}

最佳答案

定义一个计数器并将两个标识符保存到卡片的某个位置。这是伪代码的一种解决方案:

//global variable 
int flippedCardCounter = 0;
Array[] flippedCards = new Array[2];

gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
switch(flippedCardCounter)

//flippedCardCounter = 0 -> no cards flipped
case 0:
flippeCardCounter++; //increase counter
flippedCard[0] = position //save reference
startFlipAnimation(yourItemAtPosition(0));
break;
//flippedCardCounter = 1 -> one card flipped
case 1:
flippeCardCounter++; //increase counter
flippedCard[1] = position //save reference
startFlipAnimation(yourItemAtPosition(1));
break;
//flippedCardCounter = 2 -> hide two flipped card, flip one card
case 2:
flippeCardCounter = 0; //reset counter

//get reference to already flipped card and hide it
startUnFlipAnimation(yourItemAtPosition(flippedCards[0]));
startUnFlipAnimation(yourItemAtPosition(flippedCards[1]));

//reset your array ()
flippedCards = new Array[2]; //not really needed
flippedCards[0] = position
flippeCardCounter++; //increase counter
startFlipAnimation(yourItemAtPosition(position));
break;
}

});

关于java - 如何以特定的项目点击次数翻转 GridView 中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19305995/

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