English 中文(简体)
使用 GripView 中的图像设计器
原标题:Using a ImageAdapter in a GridView

I m trying to create a memory game - where I have 5x5 images on the screen and the user has to match the images. I ve been using a GridView and populate it with images using a ImageAdapter. The game works something like this: - when a user matches 2 images - the images remain on the screen - when a user fails to match the 2 pictures - the imagess changes back to the question-mark. The problem is that I can t manage to keep the pictures previously matched on the screen - when I use the notifyDataSetChanged() method - all the screen is filled again with question marks. Here s my code:

// 在图像编辑器中获取查看方法

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 张图像时的点击听器

 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();
            }
            }       
        }
    });

有人能帮忙吗?

最佳回答
// 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);
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签