在GridView中使用ImageAdapter

时间:2012-05-23 21:53:40

标签: android gridview baseadapter

我正在尝试创建一个记忆游戏 - 我在屏幕上有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();
            }
            }       
        }
    });

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分: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);