如何在Gridview运行时更改图像?

时间:2011-06-01 13:25:14

标签: android

我有一个GridView有3列和3行我想在用户点击任意两个图像时更改图像。

例如我单击第一行1和第3列图像然后我单击第3行和第2列显示现在我想要更改这两个图像像交换图像它是如何可能的?

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

        gridView = (GridView)findViewById(R.id.gridviewmy);
        gridView.setAdapter(new ImageAdapter(this));
        final ImageAdapter im = new ImageAdapter(this);
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                // TODO Auto-generated method stub
                int i=0; int j=0;
                if( i != 0){
                    j=arg2;
                    System.out.println("First Click "+j);
                }else{
                    i=arg2;
                    System.out.println("Second Click "+i);
                }
                im.getItem(arg2);
                //im.changeImage();
                Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
                System.out.println("AdapterView "+arg0);
                System.out.println("View "+arg1);
                System.out.println("Integer "+arg2);
                System.out.println("long "+arg3);
            }
        });
    }
}

class ImageAdapter extends BaseAdapter{
    private Context mContext;
    ImageView iView;
    public ImageAdapter(Context c){
        this.mContext = c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        System.out.println("Item Is :-"+mThumbIds[position].toString());
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        System.out.println("Geting Id of Item "+mThumbIds[position]);
        if(iView != null){
        iView.setImageResource(mThumbIds[0]);
        Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if( convertView == null){
            iView = new ImageView(mContext);
            iView.setLayoutParams(new GridView.LayoutParams(85, 85));
            iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iView.setPadding(8,8,8,8);
        }else{
            iView = (ImageView)convertView;
        }

            iView.setImageResource(mThumbIds[position]);
            return iView;
    }
    private Integer[] mThumbIds = {
            R.drawable.a_bhaibij,   R.drawable.a_dashera,       R.drawable.a_dipawali,
            R.drawable.a_gandhi,    R.drawable.a_holi,          R.drawable.a_indepe,
            R.drawable.a_janmastmi, R.drawable.a_kite,          R.drawable.a_newyear
            };
    public void changeImage(){
            iView.setImageResource(mThumbIds[5]);
    }
}

3 个答案:

答案 0 :(得分:4)

在GridView中交换图像非常简单。你需要做的是

1 *存储要在其中执行交换的cliked位置。

2 *通过使用这两个值,在mThumbIds数组上执行交换操作。

3 *最后调用Adapter对象上的notifyDataSetChanged()方法,即im.notifyDataSetChanged();

public class MainActivity extends Activity {
/** Called when the activity is first created. */
int i=0;
int firstClick,secondClick;
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gridView = (GridView)findViewById(R.id.gridviewmy);
    gridView.setAdapter(new ImageAdapter(this));
    final ImageAdapter im = new ImageAdapter(this);
    gridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
            // TODO Auto-generated method stub
            i++;
            if( i %2!=0){
                firstClick=arg2;
            }else{
                secondClick=arg2;
                Integer help=new Interger(mThumbIds[firstClick]);
                mThumbIds[firstClick]=mThumbIds[secondClick];
                mThumbIds[secondClick]=help;
                notifyDataSetChanged();
                System.out.println("Second Click "+i);
            }

        }
    });
}

}

class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
    this.mContext = c;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    System.out.println("Item Is :-"+mThumbIds[position].toString());
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    System.out.println("Geting Id of Item "+mThumbIds[position]);
    if(iView != null){
    iView.setImageResource(mThumbIds[0]);
    Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
    }
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if( convertView == null){
        iView = new ImageView(mContext);
        iView.setLayoutParams(new GridView.LayoutParams(85, 85));
        iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iView.setPadding(8,8,8,8);
    }else{
        iView = (ImageView)convertView;
    }

        iView.setImageResource(mThumbIds[position]);
        return iView;
}
private Integer[] mThumbIds = {
        R.drawable.a_bhaibij,   R.drawable.a_dashera,       R.drawable.a_dipawali,
        R.drawable.a_gandhi,    R.drawable.a_holi,          R.drawable.a_indepe,
        R.drawable.a_janmastmi, R.drawable.a_kite,          R.drawable.a_newyear
        };

}

我认为这可以解决你的问题。

一切顺利。

答案 1 :(得分:3)

还要执行以下操作以更新网格视图图像以完成交换操作:

im.notifyDataSetChanged();
gridView.setAdapter(im);
gridView.invalidateViews()

答案 2 :(得分:0)

notifyDataSetChanged();对我不起作用。 eclipse出错了。 所以不是搜索真正的解决方案,如果有的话,我只是重新加载了java页面。 当然,我将内部存储中gridview(适配器)中的图像状态保存在名为graphics的文件中。因此,在重新加载java页面时,它会重新绘制正确的图像。 它有效。