单击它后如何更改网格布局上的图像?

时间:2012-03-03 22:23:55

标签: android android-layout gridview

我正在尝试制作一个3x3网格布局的图像(ImageView),一旦用户点击任何图像,该图像上的照片就会发生变化,我面临的问题是我找不到参考到被点击的图像,以便我可以更改它上面的照片。我想写的游戏是一个简单的tic tac toe
1)这是我的主要活动的代码

package eg.edu.guc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
//import android.widget.Toast

public class AndXOActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridView = (GridView) findViewById(R.id.gridview);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Toast.makeText(AndXOActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                //ImageView imageView = new ImageView(getMContext());
                getImageView().setImageResource(R.drawable.x);
            }
        });
    }
}

2)以下是ImageAdapter类的代码

import android.content.Context;  
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter  {

       private Context mContext;
       public ImageView imageView;

       public ImageView getImageView() {
        return imageView;
    }

        // Keep all Images in array
        public Integer[] mThumbIds = {
                R.drawable.sample_0, R.drawable.sample_1,
                R.drawable.sample_2, R.drawable.sample_3,
                R.drawable.sample_4, R.drawable.sample_5,
                R.drawable.sample_6, R.drawable.sample_7,
                R.drawable.x, R.drawable.o

        };

        // Constructor
        public ImageAdapter(Context c){
            mContext = c;
        }
        public Context getMContext() {
            return mContext;
        }

        @Override
        public int getCount() {
            return mThumbIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mThumbIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            imageView = new ImageView(mContext);
            imageView.setImageResource(R.drawable.tile);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;
        }
    }

3)这是.xml文件

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="150dp"
    android:layout_gravity="center"
    android:layout_height="150dp"
    android:columnWidth="90dp"
    android:numColumns="3"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:gravity="center"
/>

请告诉我,一旦点击ImageView,我该怎么做才能改变图像!

2 个答案:

答案 0 :(得分:0)

1。循环遍历ImageViews本身,为它们提供所有相同的OnClickListener,但也使用您拥有的图像数组的索引标记它们。在侦听器中,获取标记,索引到数组中,执行您现在需要做的事情,因为您知道与ImageView关联的图像,更改标记。

一个例子:

@Override
public void onCreate(Bundle bundle) {
  super.onCreate(bundle);
  setContentView(R.layout.select);
  OnClickListener listener = new OnClickListener() {
    @Override
    public void onClick(View v) {
      Intent i = new Intent(SelectActivity.this, QuizActivity.class);
      i.putExtra("quiz#", ((Integer) v.getTag()).intValue());  // <------
      i.putExtra("mode", ((RadioGroup) findViewById(R.id.modeSelect)).getCheckedRadioButtonId());
      startActivity(i);
    }
  };

  int index = 0;
  for (int quiz: Quizzes.quizzes) {
    Button b = (Button) findViewById(quiz);
    b.setOnClickListener(listener);
    b.setTag(Integer.valueOf(index++)); // <------
  }
}

2。忘记ImageViews的GridView。相反,子类自己查看和绘制井字游戏板,让您的主板响应点击。在棋盘计算出点击的象限后,它可以参考其自己的游戏内部模型来决定如何进行,并且当它重新绘制时,它可以再次使用该模型重绘。我点击了什么图片?“逻辑更接近于tic-tac-toe的逻辑。如果您以前从未这样做过,请从Snake SDK示例中学习TileView

答案 1 :(得分:0)

让我们尝试一下,因为gridview中的每个项目都是ImageView

@Override
public ImageView getItem(int position) {
   return mThumbIds[position];
}

并使用该方法获得项目的正确位置

gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            parent.getItemAtPosition(position).setImageResource(R.drawable.x);
        }
    });