Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine the empty grid placeholder in the gridview and place the touched grid in the empty grid placeholder
    primarykey
    data
    text
    <p>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 <b>sliding game puzzle</b> 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 <a href="http://mypuzzle.org/sliding" rel="nofollow">The Sliding Game</a> <br/> Following is the code for this: <br> <b>Activity</b><br></p> <pre><code> 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; } } </code></pre> <p><br> <b>Adapter</b><br></p> <pre><code>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&lt;Integer&gt; randomize(int size){ Random random = new Random(); ArrayList&lt;Integer&gt; generated = new ArrayList&lt;Integer&gt;(); for(int i = 0; i &lt; 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&lt;Integer&gt; list = randomize(size); Collections.shuffle(list); Integer[] nums = new Integer[size]; list.toArray(nums); return nums; } } </code></pre> <p><br> <b>Touch Listener</b></p> <pre><code>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; } } } </code></pre> <p><b>My Question </b><br/> 1. How to determine that empty grid placeholder in the gridview? <br> 2. How to place the touched grid in the empty grid placeholder?</p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload