My app has a gridview filled with pictures (card memory game). After touch the card is flipped and the face picture is shown. But sometimes the face picture does not appear. If I do few more flips with the card, it restores.
After click I call 1/2 flip animation:
this.gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
actPosition = position;
cardsTurned++;
gridview.setEnabled(false); // prevents clicking on other cards when one is actually animated
// first part of card turn animation
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation1);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation1);
}
});
And after animation end I set a Bitmap to the right position:
@Override
public void onAnimationEnd(Animation animation) {
Bitmap bitmapFace = BitmapFactory.decodeFile(this.imageAdapter.getItem(actPosition).getBitmapFace());
Bitmap bitmapBack = BitmapFactory.decodeResource(getResources(), R.drawable.card_back);
// after animation1 should come animation2
if (animation==animation1) {
// now we see nothing, change picture to face/back
if (imageAdapter.getItem(actPosition).getBackShowed()) // if we see back
((ImageView) gridview.getChildAt(actPosition)).setImageBitmap(bitmapFace); // show face
else // if we see face
((ImageView) gridview.getChildAt(actPosition)).setImageBitmap(bitmapBack); // show back
// complete animation with the second part and show changed picture
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation2);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation2);
}
else {
if(this.cardsTurned == 1) { // 1 card is turned now
card1 = imageAdapter.getItem(actPosition); // first turned card
gridview.getChildAt(actPosition).setClickable(true); // wait for the second card
}
else if(this.cardsTurned == 2) { // 2 cards are turned now
card2 = imageAdapter.getItem(actPosition); // second turned card
// if cards are a PAIR, remove them from gridview
if(card1.getPair() == actPosition) { // turned cards are a PAIR
// remove cards
gridview.getChildAt(card1.getPosition()).setAlpha(0);
gridview.getChildAt(card1.getPosition()).setOnClickListener(null);
gridview.getChildAt(actPosition).setAlpha(0);
gridview.getChildAt(actPosition).setOnClickListener(null);
}
else { // cards are not a PAIR
// card2 turn back
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation1);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation1);
((ImageView) gridview.getChildAt(actPosition)).setImageBitmap(bitmapBack);
((ImageView) gridview.getChildAt(actPosition)).clearAnimation();
((ImageView) gridview.getChildAt(actPosition)).setAnimation(animation2);
((ImageView) gridview.getChildAt(actPosition)).startAnimation(animation2);
// card1 turn back
((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation1);
((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation1);
((ImageView) gridview.getChildAt(card1.getPosition())).setImageBitmap(bitmapBack);
((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation2);
((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation2);
imageAdapter.getItem(card1.getPosition()).changeBackShowed();
gridview.getChildAt(card1.getPosition()).setClickable(false);
}
this.cardsTurned = 0;
}
// unlock grid view for another clicking
imageAdapter.getItem(actPosition).changeBackShowed();
gridview.setEnabled(true);
}
}
my adapter's getCount() returns allways 20:
@Override
public int getCount() {
Log.d(TAG, "getCount(): " + cards.length);
return cards.length;
}
and when the picture is not shown, the adapter's getView() is also not called.
How is that possible? Is it something with gridView or am I doing something wrong?