0

I have a question regarding GridView implementation. The GridView which has been created in my project, is a 3 X 3 gridview. Each grid is consist of random numbers. This gridview is not a fully fledged 3 X 3 grid but rather there are only 8 grids in the gridview. The reason is that I am creating a sliding game puzzle wherein the user touches a grid beside the empty grid placeholder and the touched grid moves to the empty grid placeholder and the current grid becomes empty. This makes way for the other grids to be placed here when touched. Refer to The Sliding Game
Following is the code for this:
Activity

   public class SlidingGameActivity extends Activity {
    GridView gridView;
    GridAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sliding_game);
        gridView = (GridView)findViewById(R.id.gridView);
        adapter = new GridAdapter(this);
        gridView.setAdapter(adapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_sliding_game, menu);
        return true;
    }    }


Adapter

public class GridAdapter extends BaseAdapter {
    Context context;
    LayoutInflater mInflater;
    int num ;
    Integer[] numbers = {1,2,3,4,5,6,7,8} ;
    public GridAdapter(Context context){
        this.context = context;
        mInflater = LayoutInflater.from(context);
        numbers = storeRandomNumbers(8);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return numbers.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        GridDragListener dragListener = new GridDragListener();
        GridTouchListener touchListener = new GridTouchListener();
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.block, null);
            holder = new ViewHolder();
            holder.block =    (RelativeLayout)convertView.findViewById(R.id.block);
            holder.img = (ImageView)convertView.findViewById(R.id.blue_rect);
            holder.textView = (TextView)convertView.findViewById(R.id.number);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.block.setOnTouchListener(touchListener);
        holder.block.setOnDragListener(dragListener);
        holder.img.setImageResource(R.drawable.blue_rectangle);
        holder.textView.setText(String.valueOf(numbers[position]));
        return convertView;
    }

    static class ViewHolder{
        RelativeLayout block;
        ImageView img;
        TextView textView;
    }



    public ArrayList<Integer> randomize(int size){

        Random random = new Random();
        ArrayList<Integer> generated = new ArrayList<Integer>();
        for(int i = 0; i < size; i++){
            while(true){
                Integer next = random.nextInt(size) + 1;
                if(!generated.contains(next)){
                    generated.add(next);
                    break;
                }
            }
        }
        return generated;


    }

    public Integer[] storeRandomNumbers(int size){
        ArrayList<Integer> list = randomize(size);
        Collections.shuffle(list);
        Integer[] nums = new Integer[size];
        list.toArray(nums);
        return nums;

    }

}


Touch Listener

public class GridTouchListener implements View.OnTouchListener{

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");

            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.VISIBLE);
            return true;
          } else {
              view.setVisibility(View.VISIBLE);

              return false;
          }
    }

}

My Question
1. How to determine that empty grid placeholder in the gridview?
2. How to place the touched grid in the empty grid placeholder?

Please refer to the code which I have provided. If anybody has implemented this idea and has their own code or any idea or suggestion, then please provide it.

user
  • 86,916
  • 18
  • 197
  • 190
Sohaib Rahman
  • 183
  • 2
  • 19

1 Answers1

1
  1. How to determine that empty grid placeholder in the gridview?

You don't really need to determine that position, you could simply monitor that position. For example your adapter could have a int field representing the position of the empty cell. Initially this field will be 8(I'm assuming that the last cell of the GridView is the empty one at start). Whenever there is a change in the position of the empty cell(due to a drag for example) update that int field with the new position. When you need to know the empty cell position just have a look at that int field.

  1. How to place the touched grid in the empty grid placeholder?

I assume you want to do this when the user drags the touched view on the empty cell. As you start the drag the views will receive DragEvents(for which I see that you set a OnDragListener). Knowing the empty cell position(and accordingly its view in the GridView) you could monitor the empty cell's view for a DragEvent with an action of ACTION_DROP. When that happens swap the data of the dragged view with the empty cell data and call notifyDataSetChanged().

Also, keep in mind that your adapter currently doesn't have an empty cell as you have 8 numbers(probably not the best choice as you need to represent an empty cell somehow) in the array that backs it. You need to have 9 values so we have room to swap the empty cell.

user
  • 86,916
  • 18
  • 197
  • 190