Advertisement
Guest User

Untitled

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