Advertisement
Guest User

Untitled

a guest
Dec 19th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. note most of my code isn't original, but patchwork code done on the basis found here: http://developer.android.com/guide/topics/ui/layout/gridview.html and found here: http://coding4ataraxia.blogspot.com/2011/06/how-to-display-chessboard-on-android.html
  2.  
  3.  
  4. package com.bansal.hellogridview;
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.app.Activity;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.OnItemClickListener;
  13. import android.widget.GridView;
  14. import android.widget.Toast;
  15.  
  16. public class HelloGridView extends Activity {
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22.  
  23. GridView gridview = (GridView) findViewById(R.id.gridview);
  24. gridview.setAdapter(new ImageAdapter(this));
  25.  
  26. gridview.setOnItemClickListener(new OnItemClickListener() {
  27. @Override
  28. public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
  29. Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
  30. }
  31.  
  32. });
  33. }
  34.  
  35.  
  36.  
  37.  
  38. }
  39.  
  40. ---------------------------------------------------------------------------------------------------------
  41. package com.bansal.hellogridview;
  42.  
  43. import android.content.Context;
  44. import android.app.Activity;
  45. import android.view.LayoutInflater;
  46. import android.view.View;
  47. import android.view.ViewGroup;
  48. import android.widget.BaseAdapter;
  49. import android.widget.GridView;
  50. import android.widget.ImageView;
  51.  
  52. public class ImageAdapter extends BaseAdapter {
  53. private Context mContext;
  54.  
  55. public ImageAdapter(Context c) {
  56. mContext = c;
  57. }
  58.  
  59. public int getCount() {
  60. return mThumbIds.length;
  61. }
  62.  
  63. public Object getItem(int position) {
  64. return null;
  65. }
  66.  
  67. public long getItemId(int position) {
  68. return 0;
  69. }
  70.  
  71. // create a new ImageView for each item referenced by the Adapter
  72. public View getView(int position, View convertView, ViewGroup parent) {
  73.  
  74. LayoutInflater layoutInflater = ((Activity)mContext).getLayoutInflater (); // we get a reference to the activity
  75. squareContainerView =
  76. layoutInflater.inflate(R.layout.square, null);
  77.  
  78. // Background
  79. final ImageView squareView =
  80. (ImageView)squareContainerView.findViewById(R.id.square_background);
  81. squareView.setImageResource(this.mThumbIds[(position + position/8)%2]);
  82.  
  83. if (position % 2 == 0) { //mock test
  84. // Add The piece
  85. final ImageView pieceView =
  86. (ImageView)squareContainerView.findViewById(R.id.piece);
  87. pieceView.setImageResource(R.drawable.ic_launcher);
  88. pieceView.setTag(position);
  89. }
  90. }
  91. else {
  92. squareContainerView = convertView;
  93. }
  94. return squareContainerView;
  95.  
  96. }
  97.  
  98. // references to our images
  99. private Integer[] mThumbIds = {
  100. // R.drawable.sample_2, R.drawable.sample_3,
  101. // R.drawable.sample_4, R.drawable.sample_5,
  102. // R.drawable.sample_6, R.drawable.sample_7,
  103. // R.drawable.sample_0, R.drawable.sample_1,
  104. // R.drawable.sample_2, R.drawable.sample_3,
  105. // R.drawable.sample_4, R.drawable.sample_5,
  106. // R.drawable.sample_6, R.drawable.sample_7,
  107. // R.drawable.sample_0, R.drawable.sample_1,
  108. // R.drawable.sample_2, R.drawable.sample_3,
  109. // R.drawable.sample_4, R.drawable.sample_5,
  110. // R.drawable.sample_6, R.drawable.sample_7
  111. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  112. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  113. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  114. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  115. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  116. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  117. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  118. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  119. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  120. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  121. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  122. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  123. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  124. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  125. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  126. R.id.square_background, R.id.square_background, R.id.square_background, R.id.square_background,
  127.  
  128.  
  129. };
  130. }
  131.  
  132. -----------------------------------------------------------------
  133. (square.xml)
  134. <?xml version="1.0" encoding="utf-8"?>
  135. <FrameLayout
  136. xmlns:android="http://schemas.android.com/apk/res/android"
  137. android:id="@+id/square"
  138. android:layout_width="wrap_content"
  139. android:layout_height="wrap_content"
  140. android:orientation="vertical"
  141. android:gravity="center_horizontal"
  142. android:background="#00000000">
  143.  
  144. <ImageView android:id="@+id/square_background"
  145. android:layout_width="40dp"
  146. android:layout_height="35dp"
  147. android:background="#ffff0000"
  148. />
  149.  
  150. <ImageView android:id="@+id/piece"
  151. android:layout_width="wrap_content"
  152. android:layout_height="wrap_content"
  153. />
  154. </FrameLayout>
  155. ---------------------------------------------------------------------
  156. (main.xml)
  157. <?xml version="1.0" encoding="utf-8"?>
  158. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  159. android:id="@+id/gridview"
  160. android:layout_width="fill_parent"
  161. android:layout_height="fill_parent"
  162. android:numColumns="8"
  163. android:padding="1dp"
  164. android:verticalSpacing="2dp"
  165. android:horizontalSpacing="2dp"
  166. android:columnWidth="60dp"
  167. android:stretchMode="columnWidth"
  168. android:gravity="center_horizontal"
  169. />
  170. ----------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement