0

I am making an application where i need to swap images in grid view . here is the code ... how could i achieve swapping :

activity

public class GameActivity extends Activity
    implements OnTouchListener  
{

     @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Intent intent = getIntent();
    setContentView(R.layout.layout_game);

    //create grid view
    splitImage(); 
    GridView gridView = new GridView(this);
    gridView = (GridView)findViewById(R.id.grid_view);   
    final ImageAdapter images = new ImageAdapter(this,chunkedImages);
    final GridView gV = gridView;
    gridView.setAdapter(images);
    gridView.setOnTouchListener(this);

}

public boolean onTouch(View v, MotionEvent me) {

    switch(me.getAction())
    {

    case MotionEvent.ACTION_DOWN :

        Toast.makeText(GameActivity.this, "down ", Toast.LENGTH_SHORT).show();
        break;
    case MotionEvent.ACTION_UP :

        Toast.makeText(GameActivity.this, "up " , Toast.LENGTH_SHORT).show();
        break;
    case MotionEvent.ACTION_MOVE :
        //Toast.makeText(GameActivity.this, "move", Toast.LENGTH_SHORT).show();
        break;
    }
    return true;
}

 }

on touching the image it gives toast as down and on removing the fingure gives up . But how to swap image i could not understand.

evg
  • 1,308
  • 1
  • 7
  • 12
karan421
  • 863
  • 17
  • 43

3 Answers3

1

I suggest using an OnItemClicklistener where you touch two different Images to swap them. Like this:

private int firstPosition = -1;
...

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(firstPosition < 0) 
            firstPosition = position;
        else {
            Bitmap swapImage = chunkedImages.get(position);
            chunkedImages.set(position, chunkedImages.get(firstPosition));
            chunkedImages.set(firstPosition, swapImage);
            firstPosition = -1;
        }
    }
});

You should add functionality like highlighting the first image when it has been selected, but this is the basic approach.

If you want drag and drop, you are in for a lot more work...

Sam
  • 86,580
  • 20
  • 181
  • 179
  • i want to drag first image to second image and then both are swapped.../ – karan421 Aug 12 '12 at 19:50
  • Then you'll want to read [this blog](http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/) or this question: http://stackoverflow.com/q/7146639/1267661 – Sam Aug 12 '12 at 19:54
1

The proper way of swapping might be done in the adapter. Created your own base adapter or inherit from any other and include any method for swipping that will be called from the activity. Within this method you can perform the necessary changes to swap succesfully. Ultimately you'll be able to swap the items in the array by using Collections.swap

Hope it helps *Don't forget to update your component when the source is ready, if necessary (notifyDatasetChanged)

Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
0

https://github.com/thquinn/DraggableGridView

Here is a solution. may be match your requirement. Check it out

makripon
  • 66
  • 2