如何在GridView(Android)中交换图像

时间:2015-04-07 22:05:11

标签: java android android-imageview android-adapter android-gridview

我正在开发一款滑动拼图Android应用,因此我使用GridView显示代表图块的16张图片。其中一个图块将由纯白图像(代表背景)组成。这个想法是,如果用户点击与白色瓷砖相邻的任何瓷砖,则所选瓷砖和白色瓷砖将切换位置。

我试图通过将白色图块设置为用户选择的图块来实现此功能,反之亦然。我在变量masterTilePos中保存了白色瓷砖的位置(从位置15开始),并使用参考R.drawable文件中图像的ImageAdapter Integer数组,将masterValPos处的图像设置为所选索引处的图像,并将所选图像添加到白色图块。但是,当我运行程序时,磁贴只能在第一次成功切换:之后,它无法正常工作,GridView中的磁贴顺序也会被破坏。我想这可能是因为数组只是指实际的图像对象,但我不知道该怎么做呢?我遇到这个问题已经有三天了。这是我的代码:

//GRID VIEW

public class MainActivity extends ActionBarActivity { 

int masterTilePos = 15;
GridView gridview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

 //These methods check if a neighboring tile is white, if there is one,  swap() is called

   if(checkUp(position) || checkDown(position) || checkLeft(position) || checkRight(position)) { 

                swap(position);

            }

        }
    });


}


  public void swap(int pos) {

    ImageAdapter updater = new ImageAdapter(this);

  //This is where I try to switch the images, and seems to be the source of the problem

    int val = updater.mThumbIds[masterTilePos];
    updater.mThumbIds[masterTilePos] = updater.mThumbIds[pos];  
    updater.mThumbIds[pos] = val;

    updater.notifyDataSetChanged();
    gridview.setAdapter(updater);
    gridview.invalidateViews();

    masterTilePos = pos;

  }

}




//IMAGE ADAPTER


public class ImageAdapter extends BaseAdapter {

private Context mContext;


public ImageAdapter(Context c) {

    mContext = c;


}

public int getCount() {

    return mThumbIds.length;
}

public Object getItem(int position) {

    return null;
}

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;

    if (convertView == null) {

        // if it's not recycled, initialize some attributes

        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();

        int width = metrics.widthPixels / 4;
        int height = metrics.heightPixels / 4;


        imageView = new ImageView(mContext);

        imageView.setLayoutParams(new GridView.LayoutParams(width, height));

        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setPadding(1, 1, 1, 1);
    }

    else {

        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// the array containing references to the images

public Integer[] mThumbIds = {


        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
        R.drawable.pic6,
        R.drawable.pic7,
        R.drawable.pic8,
        R.drawable.pic9,
        R.drawable.pic10,
        R.drawable.pic11,
        R.drawable.pic12,
        R.drawable.pic13,
        R.drawable.pic14,
        R.drawable.pic15,
        R.drawable.background


  };

}

任何人都可以帮我解决这个问题,这样我就能成功切换网格中的图像吗?谢谢。

1 个答案:

答案 0 :(得分:4)

在onCreate中你正在做:

gridview.setAdapter(new ImageAdapter(this));

因此,您创建了适配器,而没有将其分配给任何变量。这是错的!

然后,每次交换都会创建新的适配器:

ImageAdapter updater = new ImageAdapter(this);

您将其设置为当前适配器:

gridview.setAdapter(updater);

这也是错误的。

你必须这样做:

  1. OnCreate - &gt;创建适配器,将其分配给对象的变量(属性)
  2. 然后你只需要使用那个变量。
  3. 然后,如果遇到问题,请在SWAP方法中调试逻辑。

        //GRID VIEW
    
    public class MainActivity extends ActionBarActivity { 
    
    int masterTilePos = 15;
    GridView gridview;
    ImageAdapter imageAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        gridview = (GridView) findViewById(R.id.gridview);
        imageAdapter= new ImageAdapter(this);
        gridview.setAdapter(imageAdapter);
    
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
    
     //These methods check if a neighboring tile is white, if there is one,  swap() is called
    
       if(checkUp(position) || checkDown(position) || checkLeft(position) || checkRight(position)) { 
    
                    swap(position);
    
                }
    
            }
        });
    
    
    }
    
    
      public void swap(int pos) {
    
      //This is where I try to switch the images, and seems to be the source of the problem
    
        int val = imageAdaptor.mThumbIds[masterTilePos];
        imageAdapter.mThumbIds[masterTilePos] = imageAdapter.mThumbIds[pos];
        imageAdapter.mThumbIds[pos] = val;
    
        imageAdapter.notifyDataSetChanged();
        gridview.invalidateViews();
    
        masterTilePos = pos;
      }
    }
    
    //IMAGE ADAPTER
    
    public class ImageAdapter extends BaseAdapter {
    
    private Context mContext;
    
    public ImageAdapter(Context c) {
        mContext = c;
    }
    
    public int getCount() {
        return mThumbIds.length;
    }
    
    public Object getItem(int position) {
        return null;
    }
    
    public long getItemId(int position) {
        return 0;
    }
    
    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
    
        ImageView imageView;
    
        if (convertView == null) {
    
            // if it's not recycled, initialize some attributes
            DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
    
            int width = metrics.widthPixels / 4;
            int height = metrics.heightPixels / 4;
    
            imageView = new ImageView(mContext);
    
            imageView.setLayoutParams(new GridView.LayoutParams(width, height));
    
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(1, 1, 1, 1);
        }
    
        else {
            imageView = (ImageView) convertView;
        }
    
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    
    // the array containing references to the images
    
    public Integer[] mThumbIds = {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7,
            R.drawable.pic8,
            R.drawable.pic9,
            R.drawable.pic10,
            R.drawable.pic11,
            R.drawable.pic12,
            R.drawable.pic13,
            R.drawable.pic14,
            R.drawable.pic15,
            R.drawable.background
      };
    }