0

我正在尝试创建一个记忆游戏 - 我在屏幕上有 5x5 图像,用户必须匹配图像。我一直在使用 GridView 并使用 ImageAdapter 用图像填充它。游戏的工作原理如下: - 当用户匹配 2 张图像时 - 图像保留在屏幕上 - 当用户无法匹配 2 张图片时 - 图像变回问号。问题是我无法将之前匹配的图片保留在屏幕上——当我使用 notifyDataSetChanged() 方法时——所有的屏幕都再次被问号填满。这是我的代码:

// ImageAdapter 中的 getView 方法

public View getView(int position, View convertView, ViewGroup arg2) {
     ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(4, 4, 4, 4);
        } else {
            imageView = (ImageView) convertView;
        }

        for(int i=0;i<16;i++)
        {
            if(mThumbIds[i].equals(R.drawable.ic_launcher))
            {
                imageView.setImageResource(R.drawable.ic_launcher);
            }
            else
                imageView.setImageResource(R.drawable.q_mark);
        }
        return imageView;
}

// 当用户选择 1 张图片时的 onClickListener

 gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            i++;
            Toast.makeText(easyGame.this, "" + position, Toast.LENGTH_SHORT).show();
            ImageView imgV=(ImageView)v;

            if(i%2!=0)
            {

                firstClick=position;
                imgV.setImageResource(ImageAdapter.mThumbIds[firstClick]);

            }
            else
            {   
                secondClick=position;
                imgV.setImageResource(ImageAdapter.mThumbIds[secondClick]);
            }

            if(i%2==0)
            {

            if(!(ImageAdapter.mThumbIds[firstClick].equals(ImageAdapter.mThumbIds[secondClick])))
            {
                Toast.makeText(easyGame.this, "Great!", Toast.LENGTH_SHORT).show();
                ImageAdapter.mThumbIds[firstClick]=ImageAdapter.mThumbsIdsDone[0];
                ImageAdapter.mThumbIds[secondClick]=ImageAdapter.mThumbsIdsDone[0];
                im.notifyDataSetChanged();
                gridview.setAdapter(im);
                gridview.invalidate();
            }
            }       
        }
    });

任何人都可以帮忙吗?谢谢!

4

1 回答 1

2
// declare as class variable to keep track of views which should stay visible
private HashSet<Integer> keepVisibleViews = new HashSet<Integer>(25);

//in you on click listener
if(!(ImageAdapter.mThumbIds[firstClick].equals(ImageAdapter.mThumbIds[secondClick])))
 {
     // ... the rest of your code here
     // keep track of views that should stay visible
     keepVisibleViews.add(firstClick);
     keepVisibleViews.add(secondClick);
 }


// in your getView
// show ? mark if we should, else the picture
if (keepVisibleViews.contains(position)
      setImageResource(ImageAdapter.mThumbIds[secondClick]);
else
      imageView.setImageResource(R.drawable.q_mark);
于 2012-05-23T22:28:04.473 回答